-->
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.  [ 12 posts ] 
Author Message
 Post subject: one-to-many Association Problem...
PostPosted: Thu Nov 13, 2003 12:49 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Hi guys,

I have a strange behavior while updating an object which has a related Set of objects.
I have an object "TitrePerception" which owns a Set of "Detail" objects.
THis is an unidirectional assocation and the cardinality is 0->n.

When I update the "Titreperception" object, Hibernate generates the right SQL update request, but adds another update SQL request on the table which managed Detail data.

Ex:

titrePerception.setMyProperty("blahblah");
session.saveOrUpdate(titrePerception);
session.flush();


would do:

update titre_table set (.........) where titre_id=?
AND
update detail_table set titre_id=null where titre_id=?

An exception is thrown, as the titre_id in the detail_table has a NOT NULL constraint.

We decided to set the attribute cascade="none" on the Set element, but it doesn't solve this problem.

The solution we found is to set the attribute'inverse=true' on the Set element in the mapping description file.

Is it theright solution ?
Who can explain this strange behavior ?

Thanks
Yann


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 1:00 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
read "parent / child relationship"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 1:09 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
I have read this part of the manual, and especially the part about the inverse=true system.

My question is: why does Hibernate update the "detail" table while ?

Maybe this 'weak' relationship between those 2 objects cannot be mapped as a one-to-many mapping ????
Does it exist another way to use Collections in Hibernate ?

If I missed something, give me just a direction if you can't give me the solution.
Thanks
Yann


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2003 2:02 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
As per the parent/child section

Quote:
This is not only inefficient, but also violates any NOT NULL constraint on the parent_id column.

The underlying cause is that the link (the foreign key parent_id) from p to c is not considered part of the state of the Child object and is therefore not created in the INSERT. So the solution is to make the link part of the Child mapping.
<many-to-one name="parent" column="parent_id" not-null="true"/>

(We also need to add the parent property to the Child class.)

Now that the Child entity is managing the state of the link, we tell the collection not to update the link. We use the inverse attribute.

<set name="children" inverse="true">
<key column="parent_id"/>
<one-to-many class="Child"/>
</set>


Please read http://www.hibernate.org/Documentation/InsideExplanationOfInverseTrue, you'll get your answers

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2003 6:34 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Hi Emmanuel,

Please note I don't have a bi-directional association, and I have found nothing in the Hibernate documentation about 1-n unidirectional associations...

So, I don't have any "many-to-one" element with "not null" attribute on it.
The not null constraint which throws the exception is the Database one.

Thanks to give me any tips to well-format my mapping file.

Yann


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2003 7:19 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I reformulate the ref guide.

If you have a db not-null constraint on 1-n relation, then you must do a many-to-one mapping (even if not requested in your initial mapping)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 3:30 pm 
Beginner
Beginner

Joined: Mon Nov 03, 2003 11:48 pm
Posts: 29
What about if you DON'T have the not null constraint and still want a unidirectional one to many link from parent to child. I am having problems because I can't seem to save the parent's id into the child. I am using identity generation.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 3:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
amezick wrote:
What about if you DON'T have the not null constraint and still want a unidirectional one to many link from parent to child. I am having problems because I can't seem to save the parent's id into the child. I am using identity generation.

This is a different problem, it my happen because of :
- inverse=true usage
- non cascade save-update

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 7:58 am 
Beginner
Beginner

Joined: Mon Nov 03, 2003 11:48 pm
Posts: 29
Ok, I solved it. I set inverse=false and the id saved. I have a cascade of all-delete-orphan.




Parent Mapping:
Code:
    /**
     * @hibernate.set lazy="true" where="fieldElement = 'FtirVid'" cascade="all-delete-orphan" inverse="false"
     * @hibernate.collection-key column="tar_id"
     * @hibernate.collection-one-to-many class="org.guidestar.uk.fin.hibernate.FinRevExpItem"
     *
     */
    public Set getVolIncomeDetails() {
        return this.volIncomeDetails;
    }



Child Mapping:
There is none because this is unidirectional :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 8:04 am 
Beginner
Beginner

Joined: Mon Nov 03, 2003 11:48 pm
Posts: 29
I added a comment to http://www.hibernate.org/155.html about my last post. I would really like it if something that explicit could be in the docs.
THANKS!
--Angus


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 8:37 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It is already in the odc
Parent Child Relationship chapter of the reference guide

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 8:54 am 
Beginner
Beginner

Joined: Mon Nov 03, 2003 11:48 pm
Posts: 29
I was just thinking that with all the confusion on the board about how to do this mapping it would be nice to explicitly spell it out in the docs. Something that states 'unidirectional 1toM mappings need to have inverse="flase"'. I know that if I am confused by the docs, someone else probably will be and they soon will be posting here.


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