-->
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.  [ 2 posts ] 
Author Message
 Post subject: Inserts null parent_id, then updates it.
PostPosted: Tue Nov 07, 2006 5:11 pm 
Beginner
Beginner

Joined: Fri May 12, 2006 9:58 am
Posts: 32
Hibernate version:
3.2

Mapping documents:
Code:
    <class name="model.Parent" table="parents">
        <id name="id" column="id"><generator class="native"/></id>
       
        <property name="name"/>
       
        <set name="children" order-by="id" cascade="all-delete-orphan">
            <key column="parent_id"/>
            <one-to-many class="model.Child"/>           
        </set>
    </class>

    <class name="model.Child" table="children">
        <id name="id" column="id"><generator class="native"/></id>

        <property name="name"/>
       
        <many-to-one name="parent" column="parent_id" insert="false" update="false" not-null="true" class="model.Parent" />       
    </class> 


Code between sessionFactory.openSession() and session.close():
Code:
//get the first parent from the table
Parent p = (Parent)allParents.get(0);

//create a child
Child foo = new Child();
foo.setName("foo");

//add it to the parent's set of children
p.getChildren().add(foo);

//also set the child's parent
foo.setParent(p);


Name and version of the database you are using:
MySQL or SQL Server

The generated SQL (show_sql=true):
Code:
insert into children (name, parent_id) values (?, ?)
update children set parent_id=? where id=?



Hello

These are my mappings. I create a new Child and add it in the list of children of the first Person object. My problem is that hibernate first does an insert in the children table (with parent_id NULL) and then updates that same row to set the parent_id.

Hibernate already knows which is the parent of the new child it is inserting in the database.
Is there a way to have it do that with a single query?

thanks,
Ioannis


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 2:24 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Use inverse="true" in your Set mapping.

http://www.hibernate.org/hib_docs/v3/re ... hild-bidir


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