I have a Hibernate question about globally unique objects. It is not related to a specific Hibernate version, but for completeness: I'm using version 3.2.0.ga.
The question is this: How do I create and manage objects that are unique across more than one database?
I will give an example of what I mean:
I have a Project object that links to a Template object (many-to-one). All projects I create must use the same Template object and this goes for all databases in which I create my Project objects. Also, if I move a Project from one database to another I want it to point to a Template object with the same ID and data as it did in the database it came from.
To solve this I created a static Template field with a fixed identifier in my code. But if I try to use that with Hibernate I get 'duplicate object found' messages because sometimes there is more than one Template object with the same key in memory: when a new project is created with the static constant and an old project was read from the database (along with its Template). In that case both Template objects in memory have the same identifier and field values, but they are not phisically the same object in the JVM.
Currently my workaround is to manually replace any new references to the static constant with a reference to the Template object already in the database just before saving a new Project, but obviously this is not what I want...
Does anyone have any ideas on how to correctly solve my problem?
|