-->
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: Need help with one to one unidirectional association
PostPosted: Sun Aug 06, 2006 3:38 pm 
Newbie

Joined: Sun Jul 30, 2006 3:36 pm
Posts: 4
Hibernate version:3.1

Mapping documents:
<!-- PERSON -->
<class name="com.generic.model.Person" table="person">
<id name="id" column="id" type="java.lang.Long">
<generator class="increment"/>
</id>

<one-to-one cascade="all"
name="address"
class="com.generic.model.Address"
constrained="true"
/>

<!-- ADDRESS-->
<class name="com.generic.model.Address" table="address">
<id name="personId" column="person_id">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" constrained="true"/>

Hey all

Can anyone help me out with the above mapping? I have omitted some other associations in the Person.hbm.xml file which work flawlessly, but I can't somehow map this unidirectional one-to-one association on a primary key. Whenever I call person.setAddress(....) and then personDao.save(person) I get the following stack trace:


com.generic.dao.exception.DAOException: org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: person


I have read through the Hibernate mapping and that's where I got this mapping solution from. Do I really have to have the line

<one-to-one name="person" constrained="true"/>

in Address.hbml.xml even if I never plan to retrieve Person from Address? That's what's causing the exception - but I'd rather not have a Person property in Address at all.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 7:49 am 
Newbie

Joined: Sun Jul 30, 2006 3:36 pm
Posts: 4
Any takers on this?

It seems as if Hibernate's one-to-one mapping still is rather weak at this point - as pointed out here: http://forum.hibernate.org/viewtopic.ph ... t=onetoone
Can anyone maybe give me a working example on how you got the generator class "foreign" to work?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 6:10 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
I have managed to get this to work - I seem to have done exactly as you did. Please make sure, before you test everything, that you re-create the db schema.

My Person mapping contains:
Code:
<one-to-one name="address" cascade="all"/>

My address mapping contains:
Code:
<id name="id" column="ADDRESS_ID">
   <generator class="foreign">
   <param name="property">person</param>
   </generator>
</id>
<one-to-one name="person" constrained="true"/>


My init code is:
Code:
person = new Person();
address ad = new Address();
ad.setPerson(person);
person.setAddress(ad);
session.save(person);


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.