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.