This content has been marked as final.
Show 4 replies
-
1. Re: composite pk with
heinrich Nov 7, 2005 4:16 AM (in response to danikenan)(access = AccessType.FIELD) public class ClubPK{ /* the pk fields */ private Long clubTypeId; private Long cityId; @javax.persistence.Column(name = "club_type_id") public Long getClubTypeId() { return this.clubTypeId; } @javax.persistence.Column(name = "city_id") public Long getCityId() { return this.cityId; } } @Entity(access = AccessType.FIELD) public class Club { private ClubPK key; private String description; @javax.persistence.EmbeddedId public ClubPK getKey() { return this.key; } } @Entity(access = AccessType.FIELD) public class City { @Id(generate = GeneratorType.AUTO) private Long id; private String name; } @Entity(access = AccessType.FIELD) public class ClubType { @Id(generate = GeneratorType.AUTO) private Long id; private String name; }
There is no need to put ManyToOne relations to the fields in the pk class. -
2. Re: composite pk with
danikenan Nov 7, 2005 4:59 AM (in response to danikenan)Thanks for the reply.
If I use your solution, I lose the connection between the primary key and the entities it contains.
If clubtype and city where not part of the pk I would not use a long field but rather the entitiy and the relation between the long id and the entity whould have been automatically infered by the orm framework.
Why the fact that a forign key is also part of a primary key changes the way it is mapped?
Also, I loose all the benefits of ORM and return to use the relational model as well as loose the information hiding and decoupling from the underlying schema. -
3. Re: composite pk with
heinrich Nov 7, 2005 7:36 AM (in response to danikenan)Ok, i'm sorry. I forgot something. The club class must also contain two fields for the city and clubtype objects.
(access = AccessType.FIELD) public class ClubPK{ /* the pk fields */ private Long clubTypeId; private Long cityId; @javax.persistence.Column(name = "club_type_id") public Long getClubTypeId() { return this.clubTypeId; } @javax.persistence.Column(name = "city_id") public Long getCityId() { return this.cityId; } } @Entity(access = AccessType.FIELD) public class Club { private ClubPK key; private String description; private City city; private ClubType clubType; @javax.persistence.EmbeddedId public ClubPK getKey() { return this.key; } [...] @javax.persistence.ManyToOne(fetch=FetchType.LAZY) @javax.persistence.JoinColumn(name = "city_id", insertable=false, updatable=false) public City getCity() { return this.city; } public void setCity (City city) { this.city = city; } [...] and so on } @Entity(access = AccessType.FIELD) public class City { @Id(generate = GeneratorType.AUTO) private Long id; private String name; } @Entity(access = AccessType.FIELD) public class ClubType { @Id(generate = GeneratorType.AUTO) private Long id; private String name; }
Hope this is complete now =) -
4. Re: composite pk with
danikenan Nov 8, 2005 12:15 PM (in response to danikenan)Thanks a lot for you help. You solved my problem.