-->
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: Hibernate Bi-directional @OneToOne mapping
PostPosted: Tue Mar 08, 2011 11:35 am 
Newbie

Joined: Tue Mar 08, 2011 11:08 am
Posts: 2
Hi, We have the following entity relation ship. User is the parent entity.DistrictUserDetail is the child.

@Entity
@Table(name="USERS")
public class User implements Serializable {

@Id
@Column(name="ID", nullable=false)
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
private String id;

@OneToOne(cascade= {CascadeType.ALL})
@PrimaryKeyJoinColumn(name="ID", referencedColumnName="USER_ID")
public DistrictUserDetail districtUserDetail;

... other properties..

This is the parent entity. The child entities primary key is a foreign key to User entity, and we have defined its key properties as:

@Entity
@Table(name="DISTRICT_USER_DETAIL")
public class DistrictUserDetail implements Serializable{

@Id
@Column(name="USER_ID")
@GeneratedValue(generator="foreign")
@GenericGenerator(name="foreign", strategy = "foreign",
parameters={@Parameter(name="property",value="user")})
protected String userId;

@OneToOne(mappedBy="districtUserDetail")
User user;

... other properties

So, when we create a new User, and create a new DistrictUser, and set the DistrictUser to the User and save User, we expect that the User is saved and due to cascadeAll, DistrictUser is also saved.

This is our test:

public void testSaveUserWithDistrictUserDetails() throws Exception {
User user = new User();
user.setFirstName("John");
user.setLastName("Doe");
user.setUserName("U11111");

DistrictUserDetail userDetail = new DistrictUserDetail();

userDetail.setIsLoginAllowed(Boolean.TRUE);
userDetail.setIsSuperintendent(Boolean.TRUE);

user.setDistrictUserDetail(userDetail);

User newUser = dao.save(user);

assertTrue(newUser!=null);
assertTrue(newUser.getId()!=null);
}

But we encounter the following hibernate exception:

Caused by: org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: user
at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:44)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99)
at org.hibernate.event.def.DefaultMergeEventListener.mergeTransientEntity(DefaultMergeEventListener.java:315)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:283)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:238)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:688)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:670)
at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:245)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:269)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:217)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:170)
at org.hibernate.engine.Cascade.cascade(Cascade.java:131)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
at org.hibernate.event.def.DefaultMergeEventListener.mergeTransientEntity(DefaultMergeEventListener.java:352)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:283)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:238)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:85)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:678)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:662)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:666)
at org.springframework.orm.hibernate3.HibernateTemplate$23.doInHibernate(HibernateTemplate.java:817)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 49 more

Is there a way to make this bi-directional oneToOne relationship work? If not does hibernate provide any alternatives?


Top
 Profile  
 
 Post subject: Re: Hibernate Bi-directional @OneToOne mapping
PostPosted: Tue Mar 08, 2011 11:43 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
also set the user to the detail before you save.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Hibernate Bi-directional @OneToOne mapping
PostPosted: Tue Mar 08, 2011 12:14 pm 
Newbie

Joined: Tue Mar 08, 2011 11:08 am
Posts: 2
Hi, thanks for the response but this is something I have already tried and I have tried it again now. So my test now goes like

public void testSaveUserWithDistrictUserDetails() throws Exception {
User user = new User();
user.setFirstName("John");
user.setLastName("Doe");
user.setUserName("U11111");

DistrictUserDetail userDetail = new DistrictUserDetail();

userDetail.setIsLoginAllowed(Boolean.TRUE);
userDetail.setIsSuperintendent(Boolean.TRUE);
userDetail.setUser(user);

user.setDistrictUserDetail(userDetail);

User newUser = dao.save(user);

assertTrue(newUser!=null);
assertTrue(newUser.getId()!=null);
}

but I still get the exactly same error. Any further suggestions?


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.