11

This is my annotation class and i want userId and groupId column both as primary key. I have found more questions (Question) about this, but didn't found relevant answer. I have less reputation, so I am not able to comment on posts, So I am putting my question here.

This is my code..

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.NaturalId;

@Entity
@Table(name="user_group")
public class user_group {

@Column(name="serviceProvider")
private String serviceProvider;

@Column(name="enterpriseId")
private String enterpriseId;

@Column(name="department")
private String department;

@Column(name="trunkGroupName")
private String trunkGroupName;
@Id
@Column(name="userId")
private String userId;


@Column(name="groupId")
private String group;


public String getUserId() {
    return userId;
}


public void setUserId(String userId) {
    this.userId = userId;
}


public String getGroup() {
    return group;
}


public void setGroup(String group) {
    this.group = group;
}

public String getServiceProvider() {
    return serviceProvider;
}

public void setServiceProvider(String serviceProvider) {
    this.serviceProvider = serviceProvider;
}

public String getEnterpriseId() {
    return enterpriseId;
}

public void setEnterpriseId(String enterpriseId) {
    this.enterpriseId = enterpriseId;
}

public String getDepartment() {
    return department;
}

public void setDepartment(String department) {
    this.department = department;
}

public String getTrunkGroupName() {
    return trunkGroupName;
}

public void setTrunkGroupName(String trunkGroupName) {
    this.trunkGroupName = trunkGroupName;
}


}
Community
  • 1
  • 1
amit bhardwaj
  • 883
  • 2
  • 8
  • 18
  • 4
    Possible duplicate: http://stackoverflow.com/questions/3585034/how-to-map-a-composite-key-with-hibernate – Konstantin Yovkov Jan 22 '14 at 13:27
  • i already try those solution but it's not helping. i even mapped the user_groupId class in my hibernateUtil class. but it shows this Exception: "user_group_id is not mapped [select b.userId, a.trunkGroupName from com.avis.Hibernate.user_group a, user_group_id b where b.userId='4167758224@broadconnect.ca' and a.trunkGroupName ='BC51743/BC51743_TRK01']" . that's the problem – amit bhardwaj Jan 23 '14 at 05:10
  • This has been answered but I found this post useful for a similar problem: https://vladmihalcea.com/2016/08/01/the-best-way-to-map-a-composite-primary-key-with-jpa-and-hibernate/ – Hasan K Apr 25 '17 at 16:18

2 Answers2

21

You should create a new @Embeddable class containing the PK fields:

@Embeddable
public class user_groupId implements Serializable { 
    @Column(name="userId")
    private String userId;

    @Column(name="groupId")
    private String group;
}

And use it in the @Entity as an @EmbeddedId:

@Entity
public class user_group {

    @EmbeddedId
    user_groupId id;

    ...
}

You could also use the @IdClass annotation to that effect.

This excellent answer by Pascal Thivent elaborates on the details. You can also take a look at this other answer I posted to a almost identical question some time ago.

As a side note, if you've got control over the DB structure, you might also consider avoiding composite keys. There are some reasons to do so.

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • lopez i already try this solution but it's not helping. i did the same as you said above. i even mapped the user_groupId class in my hibernateUtil class. but it shows this Exception: "user_group_id is not mapped [select b.userId, a.trunkGroupName from com.avis.Hibernate.user_group a, user_group_id b where b.userId='4167758224@broadconnect.ca' and a.trunkGroupName ='BC51743/BC51743_TRK01']" . that's the problem – amit bhardwaj Jan 23 '14 at 05:08
  • By the error you're getting it seems you're mapping the `user_groupId` class to a table in the database. Make sure it doesn't have `@Entity`, only `@Embeddable`. And why not post what you've tried in the first place instead of playing all this guessing game? – Xavi López Jan 23 '14 at 07:46
  • 2xavi because i want different answer and if i put the what i have tried then everybody suggest me on this answer only . i want different approch to do the task that's why i put the basic code but never mind i found my answer...so thanks for your help – amit bhardwaj Jan 23 '14 at 11:03
  • @amitbhardwaj Glad you've come up with a solution. Anyhow, and AFAIK, there's no other way to map a composite id with JPA than the suggested ones. Would you mind answering your own question here and accepting it so that future visitors of the question may find it helpful? There's no point in leaving the question without an accepted answer if you found it (you can also delete the question if you feel like it). – Xavi López Jan 23 '14 at 11:27
  • i used the same code what u suggested above but i add "@EmbeddedId@coloum(name = user_groupId) private user_groupId id; so that if i fire a query i can use the fields of user_groupId class in my query – amit bhardwaj Jan 24 '14 at 06:19
  • I don't really understand that. Adding @Column doesn't make much sense in this case. Remember you're mapping a composite id. You should query the id fields navigating to them with a dot, through the id property, with statements like select id.userid or id.group from user_group. – Xavi López Jan 24 '14 at 07:25
2

you can create a composite primary key in hibernate using @UniqueConstraint annotation.

@Table(name="user_group",uniqueConstraints=@UniqueConstraint(columnNames= {"userId","groupId"}))
public class user_group 
{
       @Column(name="userId")
       private String userId;

       @Column(name="groupId")
       private String group;
}

above method is not feasible if we use spring because for creating composite primary key we have to create a class is not a good thing.
in hibernate and spring you only have to create POJO classes which are available as an entity on your system.

Solanki Vaibhav
  • 449
  • 6
  • 10