Get the ID of a Child in a cascade="all" relationship, while adding it to a collection, in Hibernate
Posted
by Marco
on Stack Overflow
See other posts from Stack Overflow
or by Marco
Published on 2010-04-07T02:41:13Z
Indexed on
2010/04/07
2:43 UTC
Read the original article
Hit count: 287
Hi, i have two Entities, "Parent" and "Child", that are linked through a bidirectional one-to-many relationship with the cascade attribute set to "all". When adding a Child object to the Parent children collection using the code below, i can't get the ID of the persisted child until i commit the transaction:
Parent p = (Parent) session.load(Parent.class, pid);
Child c = new Child();
p.addChild(c);
// "c" hasn't an ID (is always zero)
However, when i persist a child entity by explicitly calling the session.save() method, the ID is created and set immediately, even if the transaction hasn't been committed:
Child c = new Child();
session.save(c);
// "c" has an ID
Is there a way to get the ID of the child entity immediately without calling the session.save() method?
Thanks
© Stack Overflow or respective owner