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/inverse on one2many
PostPosted: Sat Jan 17, 2004 10:37 am 
Beginner
Beginner

Joined: Thu Oct 30, 2003 6:29 am
Posts: 30
Location: Germany, KA
hi,

I really don't get it:

Assume I want to model a simple parent/child one to many association on a single class. A Person can have multibe SubPersons and a single SuperPerson.

Person.hbm:
<set
name="subPeople"
lazy="true"
inverse="false"
cascade="none"
>

<key
column="SUPER_PERSON_FK"
/>

<one-to-many
class="PersonStub"
/>
</set>

<many-to-one
name="superPerson"
class="PersonStub"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="SUPER_PERSON_FK"
/>

My problem is:
<code>
hibSession = hibFactory.openSession();
Person super = (Person) hibSession.load(Person.class, "A");
Person sub = (Person) hibSession.load(Person.class, "B");

sub.setSuperPerson(super);

if (sub.getSuperPerson().equlas(super))
logger.debug("that works fine");
if (!super.getSubPerson().contains(sub))
logger.debug("and that does not!");

hibSession.close();
</code>


When closing and opening a new Session it works fine!
What am I doing wrong? Do I have to set inverse to true? (caus' hibReference sais: "Changes made only to the inverse end of the association are not persisted.") Do I have to use cascade=updateOrSave?

I REALLY don't get it, can anyone explain cascade/inverse to me?

thnak you
marie


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 17, 2004 10:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you want to associate the objects, you of course have to set both sides of the association manually!

How should this
Code:
sub.setSuperPerson(super);

if (sub.getSuperPerson().equlas(super))
logger.debug("that works fine");
if (!super.getSubPerson().contains(sub))
logger.debug("and that does not!");

ever work, that is just java.

You have to do:
Code:
sub.setSuperPerson(super);
super.getSubPerson().add(sub);

if (sub.getSuperPerson().equlas(super))
logger.debug("that works fine");
if (!super.getSubPerson().contains(sub))
logger.debug("and that does not!");


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 17, 2004 11:48 am 
Beginner
Beginner

Joined: Thu Oct 30, 2003 6:29 am
Posts: 30
Location: Germany, KA
well, mhh, yes, sounds logical.

maybe i hoped that hibernate is intelligent enough to compensate my missing one :()
no, honest: after I call hibSession.save(...) I assumed that all objects were up to date. but setting both sides of the relation is maybe the easiest way. but the following problem arises: if I have a new entity and I call save() on the associated, existing entity, an Exception (unsaved entity, or s.th.) is thrown. thiswhy I wanted to avoid setting both sides.

Ok, sure, answer is: call save() on the new created entity first, but I can't. Assume the following:

static loadValues(): alters data of an entity, maybe creates a new object, maybe creates a new associated object.

save(): calls hibSession.save() on the existing entity (this), and than save() of all associated objects.

a) If loadValues() creates a new object, save() has to call hibSession.save(this) first and than hibSession.save(all associated objects).
but:
b) If loadValues() creates a new associated objects, save() has to call hibSession.save(all associated objects) first!
c) maybe both are newly created!

If I would not have to set both sides, I could call this.save() without having troubles with "unsaved entities", because the relation would not have been set explicitly.
Yes, I no, if hibernat would do manage both-side setting, I propably would have the same problem.

maybe the solution is s.th. completly diffrent (I read your tutorial (good one, thank you for that!) but I've not found a section dealing with it:)
I know, there is a possibility to alter data without persisting it and keep it over multible session. I am doing the above because I only want to save, if a user presses a save-button, because he could alter the (multi-page spanning) data and than press cancel.

dennoch vielen dank
marie


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 17, 2004 11:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You need to work with the inverse and cascade attributes. Have you read http://www.hibernate.org/hib_docs/reference/html/parent-child.html and http://www.hibernate.org/155.html ?

Probably I don't get your problem correctly, if you want send me an email (in german :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2004 6:24 am 
Beginner
Beginner

Joined: Thu Oct 30, 2003 6:29 am
Posts: 30
Location: Germany, KA
solved.
Indeed, the problem was the inverse-attribute. I set it to true and set BOTH sides of the relation.
the document http://www.hibernate.org/155.html helped alot

thanks![/url]


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.