Get the ID of a Child in a cascade="all" relationship, while adding it to a collection, in Hibernate
- by Marco
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