Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make two column as a primary key in hibernate annotation class

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


}
like image 225
amit bhardwaj Avatar asked Jan 22 '14 13:01

amit bhardwaj


People also ask

Can there be 2 columns of primary key?

Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).

How can we create primary key in Hibernate using annotations?

Mapping a primary key column with JPA and Hibernate is simple. You just need to add an attribute to your entity, make sure that its type and name match the database column, annotate it with @Column and you're done.

How do I make two columns a primary key in JPA?

A composite primary key, also called a composite key, is a combination of two or more columns to form a primary key for a table. In JPA, we have two options to define the composite keys: the @IdClass and @EmbeddedId annotations.


2 Answers

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.

like image 84
Xavi López Avatar answered Oct 05 '22 09:10

Xavi López


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.

like image 43
Solanki Vaibhav Avatar answered Oct 05 '22 09:10

Solanki Vaibhav