-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to annotate when ManyToOne is part of the composite key?
PostPosted: Tue Mar 25, 2008 5:03 pm 
Newbie

Joined: Fri Nov 10, 2006 6:37 pm
Posts: 9
[My environment: Hibernate v3.2.1 with JBoss AS 4.2.2]

Question: how to correctly annotate the entity class and IdClass for Hibernate when @ManyToOne is part of the composite key?

Following works fine with OpenJPA. However when trying with Hibernate in JBoss, I run into issues (listed after the code).

Code:
@Entity
@Table (name="user")
public class User {
   @Id
   private long uid;

   @OneToMany (mappedBy="user")
   private List <UserContact> contacts;

   ...
}


@Entity
@Table (name="userContact")
@IdClass (UserContact.UserContactId.class)
public class UserContact {
   @id
   private String name;
   
   @id
   @Column (name="userId")
   @ManyToOne
   private User user;

   public static class UserContactId implements Serializable {
     
      public string name;

      /* To make it work with OpenJPA, the advise I got on openJPA forum
       * was that a) The type "long" must match the type of primary key
       * in class User.java and b) The name "user" must match the name
       * of the variable in UserContact class.
       */
      public long user;
      ....
   }
}


Issues with Hibernate:

1) First issue was that Hibernate was not able to find column name "userid" in table UserContact. This is because I think Hibernate expects annotations inside the IdClass. So I duplicated the annotation "@Column (name="userId")" on the IdClass field "user" to get around that problem.

2) Next problem is while inserting a new row into UserContact table, it gave error about incorrect type for IdClass field "user". There was a mismatch in the code and hibernate about whether the type is "long" or "User class" for this field.

How should IdClass and Entity class be annotated to make them work with Hibernate? Since both OpenJPA and Hibernate implement the EJB3/JPA specs, I would have expected my code to work as-is in Hibernate...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 26, 2008 9:15 am 
Newbie

Joined: Fri Nov 10, 2006 6:37 pm
Posts: 9
bump

What should be the type and annotations for "user" field in Entity class UserContact and IdClass UserContactId?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 26, 2008 10:53 am 
Newbie

Joined: Fri Nov 10, 2006 6:37 pm
Posts: 9
Ok, this is what worked in Hibernate

Code:
@Entity
@Table (name="userContact")
@IdClass (UserContact.UserContactId.class)
public class UserContact {
   @id
   private String name;
   
   @id
   private User user;

   public static class UserContactId implements Serializable {
     
      public string name;

      @JoinColumn (name="userId")
      @ManyToOne
      public User user;
      ....
   }
}


And when I read the ejb3 persistence specs this looks much closer to specs than the code that worked with openJPA.

After further researching I found out that the openJPA implementation I am using in WebLogic is 0.9.7. There was a bug (openjpa-257) which was fixed in 1.0 release, which might address this inconsistency.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.