-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: ManyToMany mapping using annotations and a lookup table...
PostPosted: Mon Feb 20, 2012 5:39 pm 
Newbie

Joined: Mon Feb 20, 2012 5:26 pm
Posts: 3
I inherited a database schema where, for example, I have the following 3 tables:

USER
GROUP
USER_GROUPS

For some reason the original DBA assigned the PK to the GROUP table to be GROUP_ID and GROUP_NAME so we have a composite key on GROUP and USER_ID for the USER table.

For GROUP:
Code:
public class Group {
private GroupId id;
@EmbeddedId
public GroupId getId() { return id; }
}

@Embeddable
public class GroupId {
private Integer groupId;
private String groupName;
@Column(name="GROUP_ID")
public Integer getGroupId() { return groupId; }
@Column(name="GROUP_NAME")
public String getGroupName() { return groupName; }
}


For USER:
Code:
public class User {
private String userId;
@Id
@Column(name="USER_ID")
public String getUserId() { return userId; }
}


For USER_GROUPS:
Code:
public class UserGroup {
private UserGroupId id;
@EmbeddedId
public UserGroupId getId() { return id; }
}

@Embeddable
public class UserGroupId {
private Integer groupId;
private String userId;
@Column(name="GROUP_ID")
public Integer getGroupId() { return groupId; }
@Column(name="USER_ID")
public String getUser() { return userId; }
}


The USER_GROUPS table is a simple lookup that contains USER_ID's and GROUP_ID's such that users belong to multiple groups.

I am having just a heck of a time figurring out how to map this relationship. My latest attempt, which did not work, may provide insight into what I am trying to do:

Code:
public class User {
.
.
.
@ManyToMany
@JoinTable(name = "USER_GROUPS",
      joinColumns = {@JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")},
      inverseJoinColumns = {
         @JoinColumn(name = "GROUP_ID", referencedColumnName = "GROUP_ID"),
         @JoinColumn(name = "GROUP_NAME", referencedColumnName = "GROUP_NAME")}
)
List<Group> getGroups() { return groups; }


All I want to do is get a collection (updatable, etc.) of groups that a user is associated with. Remember, I inherited this design (I see no logical reason why the group requires an ID and a name to be unique since there are fewer than 100 groups total in the system).

Any pointers would be greatly appreciated.


Top
 Profile  
 
 Post subject: Re: ManyToMany mapping using annotations and a lookup table...
PostPosted: Mon Feb 20, 2012 11:18 pm 
Newbie

Joined: Fri Feb 17, 2012 8:20 am
Posts: 18
May be this is what you are searching...

Hibernate Many To Many Mapping Using Annotations


Top
 Profile  
 
 Post subject: Re: ManyToMany mapping using annotations and a lookup table...
PostPosted: Wed Feb 22, 2012 2:03 pm 
Newbie

Joined: Mon Feb 20, 2012 5:26 pm
Posts: 3
I decided to drop the inclusion of the group object collection in the user class and, instead, just kept the association collection since deleting a user should never delete a group but the user->group linkage should be deleted. This now works.

The new issue is from the opposite side of the equation, trying to get a collection of user->group links from the group object.

User/Group link has a PK of user_id and group_id

Code:
USER_GROUP_LINK
---------------
USER_ID      PK
GROUP_ID     PK

GROUP
---------------
GROUP_ID     PK
GROUP_NAME   PK


I'm just trying to grab all of the user/group links for a given group (I also have a group/permissions table but if I figure out one I'll figure out the other since they have similar issues).

Nothing I've tried so far has worked. I either have too many or too few columns defined in the JoinColumn/JoinTable/mappedBy elements.


Top
 Profile  
 
 Post subject: Re: ManyToMany mapping using annotations and a lookup table...
PostPosted: Thu Feb 23, 2012 12:05 pm 
Newbie

Joined: Mon Feb 20, 2012 5:26 pm
Posts: 3
SOLVED my issue by just including the USER/GROUP link as a collection inside of USER and having the USER/GROUP link class associate back to users and groups. It slows down a little but not enough to alter the flow of the application in a noticeable manner.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.