------------------------------------------------------------------------------------------------
网上提供的版本基本都是这个版本的居多,据网友测试说在oracle中测试是没问题的,在mysql中测试是失败的
@Table(name = “表名”, uniqueConstraints = {
@UniqueConstraint(name = “索引名称”, columnNames = {“字段1”,“字段2”})
}),
表加index索引
@Table(name = “表名”,indexes = {
@Index(name = “索引名称”, columnList = “字段名”)
})
------------------------------------------------------------------------------------------------------------
mysql通过设置唯一索引的方式设置的唯一约束,使指定字段的值不可重复。
一般情况下,索引类型为Normal,可以通过将索引类型设置为UNIQUE(唯一索引)
user_telPhone `tel_phone` UNIQUE BTREE
user_account `account` UNIQUE BTREE
user_email `email` UNIQUE BTREE
user_wxOpenId `wx_open_id` UNIQUE BTREE
----------------------------------------------------------------------------------------------------
以下为正题
JPA设置索引可以通过设置 @Index 的unique属性进行设置,默认是false,设置为true后,jpa自动建表是,就会将相应的字段的索引类型设置为unique。
package com.hema.autoworker.ssoserver.entity;
import BaseModelEntity.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.util.Date;
/**
* @author CrazyBody
* @version 1.0.0
* @ClassName User.java
* @createTime 2019年08月02日 22:35:00
* @Description TODO
*/
@Entity
@Table(name = "tab_user", indexes = {@Index(name = "user_telPhone",columnList = "telPhone", unique = true),
@Index(name = "user_account",columnList = "account", unique = true),
@Index(name = "user_email",columnList = "email", unique = true),
@Index(name = "user_wxOpenId",columnList = "wxOpenId", unique = true)}
)
@Data
@EqualsAndHashCode(callSuper = false)
@EntityListeners(AuditingEntityListener.class)
public class User extends BaseEntity {
@Id
@GenericGenerator(name = "system-uuid", strategy = "uuid2")
@GeneratedValue(generator = "system-uuid")
@Column(name = "id", unique = true, nullable = false, length = 36)
private String UUID;
/**
* 昵称
*/
private String nickName;
/**
* 性别
*/
private Integer gender;
/**
* 帐号
*/
private String account;
/**
* 密码
*/
private String password;
private String companyNo;
/**
* 出生日期
*/
private Date birthday;
/**
* 性别
*/
private String sex;
/**
* 真实姓名
*/
private String realName;
/**
* 联系电话
*/
private String telPhone;
/**
* 联系邮箱
*/
private String email;
/**
* 微信OpenId
*/
private String wxOpenId;
}