【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

 项目启动:

数据表中:

 

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

参考示例代码:

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


}

 

posted @ 2018-05-14 15:23  Angel挤一挤  阅读(1846)  评论(0编辑  收藏  举报