This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have two classes representing tables in my db (Contacts and ContactTtypes). I also have a cross-reference table that provides information to relate the two (ContactTypeXref).
In my Contacts class, I've created a Many-To-Many mapping using this join table like thus:
In the ContactType class I have a reverse relationship defined
In the ContactTypeXref class I have a ManyToOne relationship defined for the purpose of retrieving contact type info:
The problem I'm having is, when I update the Contact information, all of the "contact type" associations found in the cross-reference table are removed. I don't want them removed and don't understand why an update to the contact would delete those entries. Can anyone help?
How are you updating the object? If you are just creating a new Contact with the same Id and empty contactTypes are call merge(), then they must be deleted as you have removed them. To updated an object you should first read it, then modify it, then call commit, merge is not required.
Also ensure you are maintaining you bi-directional relationship correctly.
Why do you have a ContactTypeXref class? This seems very odd if you are using a ManyToMany to map the table.
Actually, the way the application works is as follows:
1) load the form view which calls a struts action (ContactAction) to get an existing contact to edit
2) after information has been updated by the user, a request is sent back to the ContactAction to "save".
3) the ContactAction sends a request to the DAO to merge the changed data with the existing data for that contact
4) Contact types that are chosen for the given contact are updated by, first, removing any existing cross-references in the ContactTypeXref table, and adding the new ones
The problem I'm having is, even when I disable the process of removing the cross-references so that I can create new ones on update of the Contact (step 4 in the list above), all cross-references are removed, though I didn't request that they be
The reason I have this cross-reference table is because there are several contact types - all users can be associated with multiple contact types and multiple sub-categories within those types. For example, there may be a "Teacher" and "Coach" contact type. Bob can be a teacher and a coach. As well, Bob may be further categorized as a "Math Teacher" and both a "Soccer" and "Track & Field" coach. This would be too much information to attach to the Contact domain object (or table). The cross-reference table keeps these relationships clean and easy to manage (take a look at http://www.java2s.com/Code/Java/JPA/ManyToManyJoinedTable.htm for an example of this type of use).
You said to "ensure you are maintaining you[r] bi-directional relationship correctly", beyond having the many-to-many relationship defined in the x-ref and Contact domain classes (something I'm already doing), what else should be done?