Hello,
I ask you kindly as I'm stuck here since days and I even couldn't figure out everything necessary after having read "Hibernate in Action".
The situation:
As on page 226 in the book, I have a many-many-association between two tables called "Projekt" and "Firmen". The link table is called "Kunden". Its primary key is a composite-key consisting of the projekt_id and firmen_id.
So far so good.
Now I have exactly the problem that's spoken about in the online
docu in chapter 22.4: Cascades and unsaved-value.
I want to create a parent and save its children at the "same" time.
For this I have a web form where a new project (-->table Projekt) can be inserted
along with the "children", in this case the clients (--> table Kunden, the link table).
At the time of saving the project, its ID is unknown. And so it's impossible save the clients with their composite-PK as one part of this primary key, namely the projekt_id (which is just now created!) is still unknown, thus null.
But now I don't know how to go one further.
I need to make the projekt (parent) persistent first, right? The I'd have to find its ID (
How do I get the last inserted ID of exactly this object?).
Afterwards I'd insert its children.
At best, I'd like to do all this in one transaction, if sensible and if possible.
I'd very appreciate your answer. Maybe this is a point which could be better treated in the book...
Hibernate version: 2.x
Mapping documents:
Code:
<class name="Projekt" table="projekte">
<id name="id" column="id" type="java.lang.Integer" unsaved-value="null">
<generator class="sequence">
<param name="sequence">sq_projekte_id</param>
</generator>
</id>
<property name="abgeschlossenAm" column="abgeschlossen_am" type="java.util.Date" />
<property name="beginn" column="beginn" type="java.util.Date" />
...
<set name="kunden" table="kunden" lazy="true" cascade="save-update">
<key column="projekte_id"/>
<many-to-many column="firmen_id" class="Firma"/>
</set>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
</class>
Code:
<class name="Firma" table="firmen" lazy="true">
<id name="id" column="id" type="java.lang.Integer" unsaved-value="null">
<generator class="sequence">
<param name="sequence">sq_firmen_id</param>
</generator>
</id>
....
<property name="email" column="email" type="java.lang.String" />
<property name="telefon" column="telefon" type="java.lang.String" />
<property name="fax" column="fax" type="java.lang.String" />
....
</class>
Code:
[b]Link table[/b]
<class name="Kunde" table="kunden">
<composite-id name="id" class="KundenKey">
<key-property name="projekt" column="projekte_id" type="java.lang.Integer"/>
<key-property name="firma" column="firmen_id" type="java.lang.Integer"/>
</composite-id>
<version name="version" column="version" unsaved-value="null"/>
^^^^^^^^^^^^^^^^^^^^^^^^^^
</class>
Code between sessionFactory.openSession() and session.close():
Transaction tx = session.beginTransaction();
session.save(p);
// FIND THE IDENTIFIER OF Parent p
// THEN INSERT ITS CHILDREN
ListIterator it = k.listIterator();
while (it.hasNext()) {
session.save((Kunde) it.next());
}
// UPDATE PARENT IF NECESSARY
session.flush();
tx.commit();
Name and version of the database you are using:
Postgres 7.4