-->
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.  [ 5 posts ] 
Author Message
 Post subject: No update unidirectional one-to-one association w join table
PostPosted: Wed Feb 16, 2011 11:05 am 
Newbie

Joined: Wed Aug 29, 2007 2:53 am
Posts: 9
I'm trying out the example from here: http://docs.jboss.org/hibernate/core/3. ... l-join-121

When I create a person, set it's address and save, all tables are filled, Person,Address and PersonAddress.
But when I set an address on an existing person and update, the address is saved but PersonAddress, the join table is not filled.
This is the mapping (I just added cascade="all"):
<class name="Person" table="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<join table="PersonAddress" optional="true" >
<key column="personId" unique="true"/>
<many-to-one name="address" column="addressId" not-null="true" unique="true" cascade="all"/>
</join>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>


And with this code :
Person person = new Person();
session.save(person);
Address address =new Address();
person.setAddress(address);
session.update(person);


I get this output:
Hibernate: insert into Person default values
Hibernate: insert into Address default values
Hibernate: update PersonAddress set addressId=? where personId=?

So the PersonAddress is updated but no insert occurs first.

Any idea what's missing?

Bart


Top
 Profile  
 
 Post subject: Re: No update unidirectional one-to-one association w join table
PostPosted: Wed Feb 16, 2011 11:40 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Hi,

There is no insert in PersonAddress when you persist the person because at that time its address is null and you specified optional="true" for the join mapping.
Then given that the many-to-one is specified as not-null="true", I guess hibernate expects the table to have a record and only tries to update it, not insert it!

You could associate the address to the person before saving the person. This way everything will be saved in one time!
But anyway why do you use a join table for a one-to-one relationship? There are easier ways to do that ...


Top
 Profile  
 
 Post subject: Re: No update unidirectional one-to-one association w join table
PostPosted: Thu Feb 17, 2011 3:53 am 
Newbie

Joined: Wed Aug 29, 2007 2:53 am
Posts: 9
Setting many-to-one as not-null="false" doesn't solve the problem.

I know setting the address before saving works, the issue is that the update doesn't work.


Top
 Profile  
 
 Post subject: Re: No update unidirectional one-to-one association w join table
PostPosted: Thu Feb 17, 2011 5:35 am 
Regular
Regular

Joined: Fri Jan 28, 2011 11:44 am
Posts: 117
Set optional to false in your join table.
But I still think that using a join table here is not a good solution!


Top
 Profile  
 
 Post subject: Re: No update unidirectional one-to-one association w join table
PostPosted: Thu Feb 17, 2011 5:41 am 
Newbie

Joined: Wed Aug 29, 2007 2:53 am
Posts: 9
I just found it out myself.Setting optional false does the trick indeed , but it adds a row each time a Person is saved.
A bit awkward in my case, i allready have a lot of Person, so the update would have to add all those id .
Thanks for the replies,

Bart


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