These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Composite foreign key within composite primary key
PostPosted: Wed Jul 20, 2011 3:07 am 
Newbie

Joined: Wed Jul 13, 2011 6:55 am
Posts: 4
Hi! I have a problem mapping a composite foreign key within a composite primary key. That's pretty complicated and probably bad db design but I'm not the owner of the database and I won't be able to change anything at all.

Here is an example of my problem:

Code:
@Entity
@Table
public class Role implements java.io.Serializable {

   @EmbeddedId
   private RoleId id;
...



Code:
@Embeddable
public class RoleId implements java.io.Serializable {

   @JoinColumns({
         @JoinColumn(name = "firstname", referencedColumnName = "firstname", nullable = false),
         @JoinColumn(name = "lastname", referencedColumnName = "lastname", nullable = false) })
   private User user;

   @Column(name = "groupid", nullable = false)
   private int groupid;
...



Now I get the following MappingException:

Code:
Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FKF61F3A3F666B95E7:role [user])) must have same number of columns as the referenced primary key (user [firstname, lastname])


When using the @AttributeOverrides annotation I can only specify one referencing column name like "firstname" (instead of both, "firstname" and "lastname":

Code:
@Entity
@Table
public class Role implements java.io.Serializable {

   @EmbeddedId
   @AttributeOverrides({
         @AttributeOverride(name = "user", column = @Column(name = "firstname", nullable = false, length = 14)),
         @AttributeOverride(name = "groupid", column = @Column(name = "groupid", nullable = false)) })
   private RoleId id;
...


Therefore I get the following exception (which of course is correct):

Code:
Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FKF61F3A3F5D0449F4:role [firstname])) must have same number of columns as the referenced primary key ((user [firstname, lastname])



Of course, nobody would use "firstname" and "lastname" as primary key, but it's just an example of my problem.


Top
 Profile  
 
 Post subject: Re: Composite foreign key within composite primary key
PostPosted: Thu Jul 21, 2011 5:38 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
I think it would be easier to help if we knew the DB schema you need to map; right the information provided, we don't know whether the problem is in what Hibernate provides or in the way you use it.

We have a setup with three tables in a master-detail-subdetail hierarchy, each level down the hierarchy adds another PK field.
We're not trying to model that using nested Id classes; MasterId, DetailId, and SubdetailId are just standard Id classes.
The foreign keys are set up separately.

We end up with a somewhat redundant POJO structure in the Detail classes: it has the PK values once in the id member (in the form of a DetailId object), and once in the form of a link to the Master object which in turn repeats most of the values in its own MasterId object.
These need to be set up consistently when creating new objects. Fortunately, you can't change PKs in a Hibernate-managed entity (Hibernate will throw up all over the place if you do that), so you don't need to worry about keeping that consistent for all updates. (Personally, I tend to set up an entity factory per entity class to keep such things under control.)
Note that this problem is very similar to having to set up the mutual linkage between master and detail POJOs.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.