Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with @ManyToMany using MapKeyClass

 
alice zhu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
schema_snapshot.jpg
[Thumbnail for schema_snapshot.jpg]
A better view for the data table relationship, thank you
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"alice NotInWonderland", please check your private messages for an important administrative matter.
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
alice zhu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic