I have an entity with a @GeneratedValue(). I know there are different generation types. My question is: Is there a generation typ which generate the ID when i create a new instance of the entity? I need the id of my object to create another object which have the id as FK, but i dont want to persist the objekt before creating the other object. Thanks for help!
You can call the store()
method. Store method doesn't persist the object in the DB as yet. It creates a proxy object in the hibernate cache layer and returns that proxy object, with the primary key populated (yes, the auto generated value is available). You can use this value for your other object as you wanted. Once done, when you flush the session, all the objects, are persisted in the DB.
This answer is now outdated/obsolete since the "store" function does not exist anymore. It needs to be deleted, but I'm unable to do so, because this is the accepted answer.