-->
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.  [ 1 post ] 
Author Message
 Post subject: foreign key references for assigned keys
PostPosted: Wed Feb 09, 2005 4:27 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 4:54 pm
Posts: 32
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.1

how does hibernate handle inserting an object tree where the top-level object has an assigned key (assigned in the database with a trigger) and is a foreign key to a child object. if i am explaining this poorly, here's a simple example. i have a class Foo, instances of which contain a set of Bar objects. the mapping for the foo table indicates that it has keys assigned by database triggers

Mapping documents:
Foo.hbm.xml:
Code:
<class name="hello.Foo" table="FOO">
    <id name="id" column="FOO_ID">
        <generator class="assigned"/>
    </id>
...
    <set name="bars" inverse="true">
        <key>
            <column name="FOO_ID" />
        </key>
        <one-to-many class="hello.Bar"/>
    </set>
</class>


Bar.hbm.xml:
Code:
<class name="hello.Bar" table="BAR">
    <id name="id" column="BAR_ID">
        <generator class="assigned"/>
    </id>
...
   <many-to-one name="foo" class="hello.Foo">
        <column name="FOO_ID" />
    </many-to-one>
</class>



in my code i could have something like this:
Code:
   Foo foo = new Foo();
   session.save(foo);
   String fooId = session.getIdentifier(foo);

and i'd be able to see the assigned id. or i could do this:
Code:
   Foo foo = new Foo();
   session.save(foo);
   session.flush();
   session.refresh(foo);

and i'd expect to see the newly assigned id in foo.

but if i do this:
Code:
   Foo foo = new Foo();
   foo.addBar(new Bar());  // add bar to bars
   session.save();


does hibernate get the foreign key reference correct here? does it go and get the generated key after persisting foo so it can set the foreign key reference in Bar's backing table correctly?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.