According to the JPA, all entities must have an identifier UNIQUE amongst all entities of its type. I'm in a situation where objects that I wish to persist all have unique identifiers, but the components of these objects do not...they only have unique identifiers amongst the other components within the object that contains them.
So, for example, if I have a "Server" object, with ID "A", which contains a CPU component with ID "1", I could also have a Server object, with ID "B", which contains a CPU with ID "1".
I can make this work for simple child components by marking the child as @Embedded and making the components themselves @Embeddable. If I have a collection of child components, I can use Hibernate and the @CollectionOfElements annotation to make this work, though this isn't a JPA standard.
What do I do about child components of these components though? You can't have @Embedded components inside @Embeddable objects so that's out. I'd also prefer not to use @CollectionOfElements so I can be ORM agnostic.
Also, auto-generating IDs doesn't seem to be ideal because if I want to update a "Server" object, I'd have to delete it first (because merges can't tell the difference between a new CPU and an old one) and, because there can be up to a 1,000 components within the parent object, that's 1,000 calls "sequence.nextVal()" calls to the database, which seems like a big waste of time, particular if I have 1,000 of objects that I'm persisting.
The only thing I could come up with was to pass the parent's ID to all of its children, so that a child's ID could create it's ID based upon its parents and its own (ie CPU ID "A1", CPU ID "B1"). This causes you to change your classes drastically, though, and it seems that an ORM technology should be able to handle this kind of situation.
Any ideas?
[Message sent by forum member 'wfsaxton' (wfsaxton)]
http://forums.java.net/jive/thread.jspa?messageID=240809