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