【hibernate/JPA】对实体类的的多个字段建立唯一索引,达到复合主键的效果【spring boot】注解创建唯一索引和普通索引...

对实体类的的多个字段建立唯一索引,达到复合主键的效果

package com.sxd.swapping.domain;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

/**
 * 多个字段 key2 和key3 建立唯一索引,需要 这两个字段都nullable = false,才能创建成功
 * 也可以创建普通索引,例如user_name 或者password
 */
@Entity
@Table(name = "model_entity",
        uniqueConstraints = {
            @UniqueConstraint(columnNames = {"key2","key3"})
        },
        indexes = {
            @Index(columnList = "user_name"),
            @Index(columnList = "password")
        })
@Getter
@Setter
public class ModelEntity{


    @Id
    @Column(length = 36)
    private String key1;

    @Column(nullable = false,length = 32)
    private String key2;

    @Column(nullable = false,length = 32)
    private String key3;

    @Column(name = "user_name")
    private String userName;

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

}
View Code

 项目启动:

数据表中:

【hibernate/JPA】对实体类的的多个字段建立唯一索引,达到复合主键的效果【spring boot】注解创建唯一索引和普通索引..._第1张图片

【hibernate/JPA】对实体类的的多个字段建立唯一索引,达到复合主键的效果【spring boot】注解创建唯一索引和普通索引..._第2张图片

 

=====================================================================================================================

参考示例代码:

@Entity
@Table(
        uniqueConstraints = {
                @UniqueConstraint(columnNames = "uid")
        },
        indexes = {
                @Index(columnList = "tenementId"),
                @Index(columnList = "tenementId,depotCode")
        })
public class Depot  extends BaseDomain{


}

 

转载于:https://www.cnblogs.com/sxdcgaq8080/p/9036249.html

你可能感兴趣的:(【hibernate/JPA】对实体类的的多个字段建立唯一索引,达到复合主键的效果【spring boot】注解创建唯一索引和普通索引...)