facebook

Hibernate annotations + composite primary key

  1. MyEclipse IDE
  2.  > 
  3. Off Topic
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #245727 Reply

    Ollie
    Member

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

    }

    #245751 Reply

    Riyad Kalla
    Member

    Moving to OT > Soft Dev

    #245761 Reply

    snpe
    Member

    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)

    #313782 Reply

    bkrakesh
    Member

    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.

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Hibernate annotations + composite primary key

You must be logged in to post in the forum log in