This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

problem with @ManyToMany using MapKeyClass  RSS feed

 
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
 
Sheriff
Posts: 67261
170
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.
 
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!
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!