| Author |
Message |
|
|
Post subject: Hibernate annotations + composite primary key
Posted: Jan 31, 2006 - 10:41 AM
|
|

Joined: Aug 04, 2005
Posts: 16
|
|
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;
}
} |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Jan 31, 2006 - 04:28 PM
|
|
Registered Member

Joined: Jan 06, 2004
Posts: 23824
|
|
Moving to OT > Soft Dev |
_________________ Riyad
MyEclipse Support
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Jan 31, 2006 - 05:44 PM
|
|
Quality Board

Joined: Jul 05, 2003
Posts: 544
|
|
Ollie ,
2 primary keys for table is impossible in relational database theroy - it isn't hibernate or myeclipse thing
you can have 1 primary key and 1 unique key (annotation UniqueConstraints )
you can't make composite index with two properties and annotations - you have to make class for composite primary key (this feature exists with xml mapping but no with annotations)
it mean, use unique constraint or make class (ID annotations can't be used twice) |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Jan 18, 2011 - 08:36 AM
|
|
Joined: Jan 18, 2011
Posts: 1
|
|
|
|
|
 |
|
|
| |