Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Map Key ManyToMany Relationships
Map Key ManyToMany Relationships [message #734779] Sun, 09 October 2011 18:03 Go to next message
Lucas Manzke is currently offline Lucas Manzke
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

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

    @MapKeyColumn
    @JoinTable(joinColumns={@JoinColumn(name="groups")})
    @ManyToMany(targetEntity=TUser.class, mappedBy="groups", cascade= CascadeType.ALL)
    private Map<TUser, Integer> users;

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

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

...

getters and setters omitted.



but this does not work.
After
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)
        
p.persist(group);


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
Call: ALTER TABLE USERGROUP ADD CONSTRAINT FK_USERGROUP_mapped_KEY FOREIGN KEY (mapped_KEY) REFERENCES TUSER (ID)
Query: DataModifyQuery(sql="ALTER TABLE USERGROUP ADD CONSTRAINT FK_USERGROUP_mapped_KEY FOREIGN KEY (mapped_KEY) REFERENCES TUSER (ID)")

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,

Lucas
Re: Map Key ManyToMany Relationships [message #735363 is a reply to message #734779] Tue, 11 October 2011 11:31 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
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.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Map_Key_Columns_.28JPA_2.0.29


James : Wiki : Book : Blog : Twitter
Previous Topic:@Array doesn't seem to support basic types
Next Topic:Map Key ManyToMany Relationships
Goto Forum:
  


Current Time: Thu Aug 21 16:13:07 EDT 2014

Powered by FUDForum. Page generated in 0.01748 seconds