Post new topic   Reply to topic
Author Message
Ollie

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;
}

}
 
 View user's profile Send private message  
Reply with quote Back to top
support-rkalla
Registered Member
Registered Member
Joined: Jan 06, 2004
Posts: 23817

Moving to OT > Soft Dev

_________________
Riyad
MyEclipse Support
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
snpe
Quality Board
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)
 
 View user's profile Send private message  
Reply with quote Back to top
bkrakesh

Joined: Jan 18, 2011
Posts: 1

I just went through one article , which explain implementation of composite primary key using annotation with simple example.
http://j2eereference.com/2011/01/implementing-composit-primary-key-with-jpa-and-hibernate/

Hope this will help you.
 
 View user's profile Send private message  
Reply with quote Back to top
All times are GMT - 6 Hours
Post new topic   Reply to topic
® 2014 - Genuitec, LLC.