-->
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: @OneToOne, @PrimaryKeyJoinColumn and Two Problems
PostPosted: Sun Mar 30, 2008 8:23 pm 
Regular
Regular

Joined: Sun Sep 26, 2004 9:27 pm
Posts: 75
Location: Atlanta, GA, USA
I'm using Hibernate 3.2.5 and Annotations 3.3.0.

I have two classes/tables that share a primary key. They are the UserAccount and UserProfile classes.

Code:
@Entity
public class UserAccount
{
   /** Primary key of this user.  */
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id ;


Code:
@Entity
public class UserProfile
{
   /** Primary key of this profile, shared by User object */
   @Id
   private long               id;

   /**
    * User to which this balance pertains. Must be set during UserProfile creation, as both objects share a primary key. The OneToOne
    * relationship from UserProfile to User should not cascade.
    */
   @OneToOne
   @PrimaryKeyJoinColumn
   private UserAccount        userAccount;


I have two problems, and they are probably related.

First, should the primary key of the second class (UserProfile) be automatically set? I'm saving my UserAccount first, and it works correctly. If I set the UserAccount on a UserProfile object, and save it, the key on UserProfile is not properly set.

I've gotten around this by setting it myself. However, I've had this working before automagically, and would like to see it with annotations.

My second problem deals with a HQL select statement not automatically joining these two tables. The following HQL does not work.

Code:
select ua from UserAccount as ua, UserProfile as up where up.alias = 'User Test 1'


It generates the following SQL code. Note how the primary keys are not joined.

Code:
select
  useraccoun0_.id as id112_,
  useraccoun0_.createdOn as createdOn112_,
  useraccoun0_.emailAddressId as emailAdd5_112_,
  useraccoun0_.suspended as suspended112_,
  useraccoun0_.username as username112_
from
  UserAccount useraccoun0_,
  UserProfile userprofil1_
where
  userprofil1_.alias='User Test 1'


If I change the HQL to the following, it does work.

Code:
select ua from UserAccount as ua, UserProfile as up where up.alias = 'User Test 1' and ua.id = up.id


However, I feel as if this should be unnecessary.

Any help appreciated.

Lukas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 5:52 pm 
Newbie

Joined: Sun Mar 02, 2008 3:43 pm
Posts: 9
For your first problem:

Code:
@org.hibernate.annotations.GenericGenerator(name="UserProfile",strategy="foreign",
      parameters={@org.hibernate.annotations.Parameter(name="property",value="userAccount")
})
@Entity
public class UserProfile
{
@Id
   private long               id; @GeneratedValue(generator="UserProfile")


for the second, and anyway I would map the relation bidirectionnaly

in UserAccount

Code:
@OneToOne
   @PrimaryKeyJoinColumn
   private UserProfile userProfile ;


in UserProfile

Code:
@OneToOne(mappedBy="userProfile")
  private UserAccount userAccount;


Not sure it has any relevence witch way it is mappedby.

So your hql request would be something like that

Code:
select ua from UserAccount as ua where ua.userProfile.alias = 'User Test 1'


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.