nordborg wrote:
... (depending on which id generator that is used) ...
Thanks for the tip :)
The problem is related with strategy of generating ids.
When using @GeneratedValue (strategy = GenerationType.IDENTITY) with PostgreSQL database, ID column is bigserial (for long Java type). When you add records to the database, ID values are automatically generated based on the sequence
tablename_id_seq. (I think using MySQL with AUTO_INCREMENT generates a similar problem)
Inside session.save() hibernate assigned ID for the entity. In the case, to get ID, entity must be added to the database.
Changing strategy to sequence solve only insert-update problem. In save method there is only one query for next sequence number and insert command is generated in commit method, but it doesn't contain values set in pre-insert listener.
At this moment a have two options:
1) Use pre-insert with identity id generator and insert-update chain of commands
2) Use save event listener.
I think second option is better :)