Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Map Key ManyToMany Relationships
Map Key ManyToMany Relationships [message #734783] Sun, 09 October 2011 22:03 Go to next message
Lucas Manzke is currently offline Lucas ManzkeFriend
Messages: 2
Registered: October 2011
Junior Member
Hey Guys,

I am new to EclipseLink and I am not making progress in solving the following problem.
I tried searching google, but I could not find a solution. Maybe I looked for the wrong keywords?

Anyways, here is the problem I am stuck with:
I am working on a little web application that is backed up by an EclipseLink persistence unit. I want users to be able to log into that application and organize in groups. However, this shall be optional, so a user can use that application without having to join any group. If they do, they will be assigned an access level inside of that group. Moreover, one user can join multiple groups, which makes this a classical ManyToMany relationship.

So this is what I imagine:
I have two entities, TUser (to avoid the user keyword) and UserGroup. They have some basic attributes, but these don't pose any problems to EclipseLink. But here comes the tough (for me) part:

I realized the rights inside a group via a HashMap. When a user joins the group or is manually assigned a certain access level, that access level (an Embeddable class only containing an int as an access mask) is associated with him in the HashMap via put(TUser, access).
Moreover, the user entity contains a list of the groups he or she is in.
What I would like EclipseLink to do is map that very list to the KeySet of the HashMap. The result I'd love to see is a table created with the two id's of the entities and the access mask that the user has inside a certain group.
Logically, I do not see a reason why this should not be possible.

I have tried something along the lines of

public class UserGroup implements Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ManyToMany(targetEntity=TUser.class, mappedBy="groups", cascade= CascadeType.ALL)
private Map<TUser, Integer> users;

public class TUser implements Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@ManyToMany(cascade= CascadeType.ALL)
private List<UserGroup> groups;


getters and setters omitted.

but this does not work.

TUser user = new TUser();
UserGroup group = new UserGroup();
group.addUser(user); //puts the user into the hashmap and adds the group to the user's group list via addGroup(this)


nothing happens (no entries in the database). There is no exception either, which leaves me clueless.
There is something I noticed though:
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Key column 'mapped_KEY' doesn't exist in table
Error Code: 1072

I found the reason why the Database(mysql) complains:
The mapped_KEY column resides in the TUSER_USERGROUP table, not USERGROUP. Thus it fails.

My question: What do I do wrong? What is the solution to my problem?
Thanks for any help.

Best Regards,

Re: Map Key ManyToMany Relationships [message #735394 is a reply to message #734783] Tue, 11 October 2011 15:31 Go to previous message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
Are you sure you are not handle the exception that is being thrown? Turn logging on finest to see what is occurring.

You mapping is wrong, first, if the mapping uses a mappedBy it cannot define a JoinTable (join table must be in the owner).
Second, for a ManyToMany to value must be the target of the collection, not the key.
What you have is still possible, but you need to use an ElementCollection to the Integer with a @MappedKeyJoinColumn to the TUser.
Also, having the owning mapping knowing nothing about the integer value is not a good idea, as it will not be able to persist it.

You would probably be best off define an Entity that maps to the join table instead.


James : : :
Previous Topic:Map Key ManyToMany Relationships
Next Topic:Eclipselink JPA merge problem
Goto Forum:

Current Time: Wed Jun 20 17:32:17 GMT 2018

Powered by FUDForum. Page generated in 0.02192 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top