I read the Hibernate Envers doc, but didn’t understand how I can get the following to work:
I have two entities: Address and Order. Order has an Address.
When an order is created, I want it to stick to the current address revision, so if the same address changes (generating a new revision), the order still points to the address revision it had when it was created.
How can I achieve this?
That’s not possible with Envers, and in fact that’s not what Envers is made for.
In your case you have two distinct Address “identities” – the old one and the new one. Hence they should be separate entities. It’s not the same Address entity that changes.
You could then use Envers to keep track of how the relation changes. Say you have order O, and address A1 and A2. Initially the relation is: O->A1. But then when the address changes, you can change the relation to: O->A2, and that will be reflected in history.
However, when you read the entities, you always get the newest version. A “current” entity can’t point to a historic entry.