users@glassfish.java.net

Re: help.. how to do many to many self reference with only half of a combine PK

From: <glassfish_at_javadesktop.org>
Date: Tue, 13 Nov 2007 06:52:50 PST

Hello,

I'm not exactly sure what you mean, but I think the spec forces using the full pks because each entry in the relation table needs to match up with specific entries. You will definetely have problems if one relation table entry matches multiple entities - if relationship_id is not unique, since the provider has to maintain them.
Adding entities to a relationship with non-unique relationship_id will cause multiple entries having the same relationship_id in the relationship_table, and also cause unknown results when the provider goes to read them back - you may get a list with X objects in it, all of the same instance instead of X different instances. You will also have problems when trying to read back: ie:
child_id | parent_id
1 | 1
1 | 1
1 | 1

relationship_id 1 could represent any number of entities, or only 1, there is no way for a JPA provider to tell. If you try to remove one object from the M:M, the provider will attempt to remove the corresponding relationship table entry, which will remove 1 or all entries in the table on you.

I would strongly advise that you try to find a unique key for use in your relationship tables, or plan on using non-JPA queries to query on the relationship.

Best Regards,
Chris
[Message sent by forum member 'chris_delahunt' (chris_delahunt)]

http://forums.java.net/jive/thread.jspa?messageID=245204