File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes problem with @ManyToMany using MapKeyClass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "problem with @ManyToMany using MapKeyClass" Watch "problem with @ManyToMany using MapKeyClass" New topic
Author

problem with @ManyToMany using MapKeyClass

alice zhu
Greenhorn

Joined: Jan 27, 2010
Posts: 5
Hello, I have a ManyToMany relationship between two tables: Xa_ Name (which represents a person) and WR_Engineering_Group
-------------------
Table. Xa_Name
Xa_Name_Id (int, pk)
Full name (String)
--------------------

---------------------
Table. WR_Engineering_Group
Id (int, pk)
group_name (String)
-----------------------------

However, the joint table has an extra column to indicate if the person is an activate authority of the group as following:
-------------------
Table. WR_Engineering_Group_Authority
Xa_Name_Id (int, pk) Reference Xa_Name.xa_name_Id
group(int, pk) Reference WR_Engineering_Group_Id
active(boolean, extra column)
--------------------

I use the following mapKeyClass method to join the two tables, and wish to use the attribute "active" to map the corresponding list of persons.

The GroupAuthorityStatus Class is just a embeddable class to wrap the extra column "active". The following is the implementation for this class


That is to say, given a group, I wish to map all the active authorities to a GroupAuthorityStatus that represents "True".

Now, it works fine to map the corresponding authorities and to add a new authority.
However, if I add a new active person as authority for a given group, the previously existing active person will be replaced by this new authority. Same problem occurs with adding a new inactive authority. It seems like only one person can be associated with the key(GroupAuthorityStatus). How could I change it to map a list of perons (List<XaName>) to the key(GroupAuthorityStatus).
The following is the method of activating a previously inactive authority. I remove the person first, and then add the person with true for its activity.
This works fine as long as there is only one person associated with the targeting status. If there already exists one person with the same status, the old person gets replaced by this incoming authority.


Any hint and help is greatly appreciated. Thank you very much for your time in advance.
By the way, I got the format of such mapkeyclass method from the following website:
google book for JPA2
MapKeyColumns from wiki



[Thumbnail for schema_snapshot.jpg]

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

"alice NotInWonderland", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
You can't use the active flag as a Map key if it is not unique. You seem to want sub kind of nested collection, i.e. Group - xNames Map<boolean active, List<XName>>.

JPA does not allow that, you can't map nested collections without an intermediate object.
You need to create an object that represents the join table,
i.e.
either,
GroupAuthorityStatusXName is an Entity and maps to the join table, Group has a 1-m to GroupAuthorityStatusXName, GroupAuthorityStatusXName has a m-1 to XName

or,
GroupAuthorityStatusXName is an Embeddable, Group has a ElementCollection to GroupAuthorityStatusXName, GroupAuthorityStatusXName has a m-1 to XName

See,
http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns

and,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Nested_Collections.2C_Maps_and_Matrices


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
alice zhu
Greenhorn

Joined: Jan 27, 2010
Posts: 5
James Sutherland wrote:You can't use the active flag as a Map key if it is not unique. You seem to want sub kind of nested collection, i.e. Group - xNames Map<boolean active, List<XName>>.

JPA does not allow that, you can't map nested collections without an intermediate object.
You need to create an object that represents the join table,
i.e.
either,
GroupAuthorityStatusXName is an Entity and maps to the join table, Group has a 1-m to GroupAuthorityStatusXName, GroupAuthorityStatusXName has a m-1 to XName

or,
GroupAuthorityStatusXName is an Embeddable, Group has a ElementCollection to GroupAuthorityStatusXName, GroupAuthorityStatusXName has a m-1 to XName

See,
http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns

and,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Nested_Collections.2C_Maps_and_Matrices





Thank you very much, James! I tried it, and it works really well with the JPA. I mean, I used the ElementCollection strategy. I didn't know the nested collection is not supported in JPA. Thanks a lot for your time and suggestion!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with @ManyToMany using MapKeyClass