-->
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: Cascade one-to-many doesnt work automatically ?
PostPosted: Fri May 07, 2004 7:48 am 
Newbie

Joined: Wed May 05, 2004 9:48 am
Posts: 8
Hi,

i have these too mapings:

<class name="hibernate.test.Person" table="person2">

<id name="id" type="long" unsaved-value="null" >
<generator class="sequence">
<param name="sequence">person2_sequence</param>
</generator>
</id>

<version name="version" type="long"/>

<property name="name"/>
<property name="salutation"/>

<set name="address"
inverse="true"
cascade="all">
<key column="person_id"/>
<one-to-many class="hibernate.test.PersonAddress"/>
</set>
</class>

and

<class name="hibernate.test.PersonAddress" table="person2address">

<id name="id" type="long" unsaved-value="null" >
<generator class="sequence">
<param name="sequence">person2address_sequence</param>
</generator>
</id>

<version name="version" type="long"/>

<property name="street"/>
<property name="city"/>

<many-to-one name="person" column="person_id" not-null ="true"/>

</class>

And code:

Person thePerson = (Person)session.load(Person.class, new Long(35));

for (int j = 0; j < 5; j++)
{
PersonAddress addr = new PersonAddress();
addr.setStreet("a");
addr.setCity("b");

thePerson.addAddress(addr);
session.save(addr); //!!! this must be called explicitly ?
}

session.flush();


And if the line session.save(addr); is not called explicitly I dont on flush error:

a different object with the same identifier value was already associated with the session: 0, of class: hibernate.test.PersonAddress

and its not runing the select from sequence. But this example is exactly the same as in help (chapter 16.3 - cascade="all" should do this for me). What can be wrong so I do not have to call the save ecplicitly. More strange is, that if I do not load the person, but create new one, I have to also call save(person) explicilty but before any address is save(addr)ed because the hibernate is saving in bad order (first in address table -> constraing violation).

Any hints please ?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2004 8:00 am 
Newbie

Joined: Wed May 05, 2004 9:48 am
Posts: 8
I forgot the implementation of addAddress:

public void addAddress(PersonAddress newAddress)
{
newAddress.setPerson(this);
address.add(newAddress);
}

Anohter thing is, that if I delete the address from set, then its working ok, the object are deleted from db (if I use cascade="all-delete-orphan") and I dont have to call session.delete(addr) explicitly.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 07, 2004 8:59 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
I don't see the problem with your map yet, but no, you don't have to explicitly call save on each child if cascade=all.
It is probably not related, but I typically use tx.beginTransaction, and tx.commit() instead of sess.flush...

James


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 09, 2004 11:30 am 
Newbie

Joined: Wed May 05, 2004 9:48 am
Posts: 8
Well I tried it also with the Transaction tx; ... But the same result ... Its very annoying I can;t figure it out ... trying for 2 days ... :((


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 10, 2004 12:17 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Check your POJO impementation (defautl values) and your equals/hashCode implementation.
http://www.hibernate.org/109.html[/url]

_________________
Emmanuel


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.