Hi,
I am using hibernate annotations and have a database model with two tables User and Group. User has 3 attibutes:
username, pass and email. The other table Group also takes username and the name of the group. Due to the existing databse model I cannot make any changes to the tables. However I need to insert multiple users to multiple groups – in a essense a many to many relationship.
The problem I am not allowed to apply a relational table for the 2 primary keys. I was thinking that I could perhaps apply a composite primary key in the group table. Although this will lead lead to redundance, that is not an issue here as I am not allowed to change the database model in anyway.
I use the two classes below to create the composity primary key (memberof + username). Unfortuneately this doesn’t work – I would be very grateful for any suggestions.
@Entity(access = AccessType.PROPERTY)
@IdClass(GroupFormPk.class)
public class GroupForm extends ActionForm {
//Integer id;
String username;
String memberof;
List groupList;
String[]groupStrings;
@Id public String getMemberof() {
return memberof;
}
public void setMemberof(String memberof) {
this.memberof = memberof;
}
@Id public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Transient
public List getGroupList() {
return groupList;
}
public void setGroupList(List groupList) {
this.groupList = groupList;
}
@Transient
public String[] getGroupStrings() {
return groupStrings;
}
public void setGroupStrings(String[] groupStrings) {
this.groupStrings = groupStrings;
}
}
@Embeddable
public class GroupFormPk implements Serializable {
String username;
String memberof;
public String getMemberof() {
return memberof;
}
public void setMemberof(String memberof) {
this.memberof = memberof;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}