Dear all,
When I persist an entity I need to know a unique id for it
before commiting the transaction. As stated in another
thread, the id of an entity can only be obtained via getId()
after commiting – at least if you want your code to be portable.
In order to overcome this, I want to add another field to my entity, getReference(), say, which stores an id which I manually set before calling session.save(). For this, I need a way to generate unique id's and I thought, I could use one of Hibernate's generators. How could I do this?
I need something like
UniqueIdSource.getNext()
which returns a new id each time it is called; for instance, it increments the current value and returns it. It should be synchronized and persist the id.
Can somebody give me a pointer on which Hibernate generator I could use for this and how?
It is important that UniqueIdSource.getNext() runs in its "own" transaction – not that two concurrently running sessions obtain the same id because neither of them has committed yet.
Thanks,
hbf
P.S. I am using Hibernate with Spring and a single data source.