Java 如何在hibernate注释类中将两列作为主键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21284175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:53:46  来源:igfitidea点击:

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

javahibernatecomposite-primary-keyhibernate-annotations

提问by amit bhardwaj

This is my annotation class and i want userIdand groupIdcolumn 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.

这是我的注释类,我想要userIdgroupId列都作为主键。我发现了更多关于此的问题(问题),但没有找到相关答案。我的声誉较低,所以我无法对帖子发表评论,所以我在这里提出我的问题。

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


}

采纳答案by Xavi López

You should create a new @Embeddableclass containing the PK fields:

您应该创建一个@Embeddable包含 PK 字段的新类:

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

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

And use it in the @Entityas an @EmbeddedId:

并将其@Entity用作@EmbeddedId

@Entity
public class user_group {

    @EmbeddedId
    user_groupId id;

    ...
}

You could also use the @IdClassannotation to that effect.

您也可以使用@IdClass注释来达到这种效果。

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

Pascal Thivent 的这个出色回答详细说明了细节。您也可以看看这个其他答案,我前一段时间发布到一个几乎相同的问题。

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.

附带说明一下,如果您可以控制数据库结构,您也可以考虑避免使用复合键。这样做是有原因的

回答by Solanki Vaibhav

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

您可以使用 @UniqueConstraint 注释在休眠中创建复合主键。

@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.

如果我们使用spring,上述方法是不可行的,因为对于创建复合主键,我们必须创建一个类并不是一件好事。
在 hibernate 和 spring 中,您只需要创建可作为系统上的实体使用的 POJO 类。