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

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

项目启动:

数据表中:

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

参考示例代码:

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

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

  1. @NamedEntityGraphs --JPA按实体类对象参数中的字段排序问题得解决方法

    JPA按实体类对象参数中的字段排序问题得解决方法@Entity @Table(name="complaints") @NamedEntityGraphs({ @NamedEntit ...

  2. idea hibernate jpa 生成实体类

    0,添加mysql数据库连接 1,生成个hibernate.cfg.xml 2,打开Persisitence 3,Import Databases Schema 4,选择表生成实体类

  3. Hibernate jpa 在实体类中对于时间的注解

    在时间类型DATE 属性上添加一个 @Temporal(TemporalType.DATE)(精确到年月日)@Temporal(TemporalType.TIME)(精确到时分秒)@Temporal( ...

  4. 【hibernate/JPA】注解方式实现 复合主键【spring boot】

    1>hibernate/JPA实现复合主键的思路:是将所有的主键属性封装在一个主键类中,提供给需要复合主键的实体类使用. 2>主键类的几点要求: . 使用复合主键的实体类必须实现Seria ...

  5. hibernate 非xml实体类配置方法!

    hibernate 非xml实体类配置方法! 这个是hibernate.cfg.xml配置文件 <?xml version='1.0' encoding='UTF-8'?> <!DO ...

  6. 在Intellij IDEA下通过Hibernate逆向生成实体类

    前言:在IDEA中,通过相关插件,可以利用Hibernate逆向生成数据表对应的实体类.具体操作及注意事项见本篇随笔. 1.创建一个基于maven的hibernate工程.并在工程中添夹hiberna ...

  7. hibernate 反向生实体类 and 为什么老是多一个id

    hibernate 反向生实体类 and 为什么老是多一个id 2017年04月01日 20:32:51 阅读数:548

  8. Entity Framework中的实体类添加复合主键

    使用Code First模式实现给实体类添加复合主键,代码如下: using System; using System.Collections.Generic; using System.Compon ...

  9. Mybatis中resultMap的作用-解决实体类属性名和数据库字段不一致

    解决实体类属性名和数据库字段不一致

随机推荐

  1. 微信小程序--动态添加class样式

    尺寸单位: rpx(responsive pixel): 可以根据屏幕宽度进行自适应.规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则750rpx = ...

  2. # Including Artificial Intelligence in a Routing ProtocolUsing Software Defined Networks

    Abstract: 问题:AI在路由协议上的应用仅适用于真实设备,尤其是无线传感器节点 The inclusion of artificial intelligence (AI) can improv ...

  3. select chosen 禁用下拉框某一个option

    $("#tbParBudCode option[value='" + budCodeId + "']").attr("disabled", ...

  4. jquery select chosen 动态绑定值

    $("#ddlMstData").find("option[value=" + data.MstKey + "]").attr(" ...

  5. windows 10 change default program bug

    windows 10 change default program bug https://www.isumsoft.com/windows-10/how-to-change-or-set-defau ...

  6. Vuex, api, SSR, module

    vuex https://vuex.vuejs.org/zh/guide/actions.html 单向数据流 单例模式 & 多个组件共享状态 State & 状态注入 Vue.use ...

  7. NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet

    springMVC 内嵌jetty时,出现了NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet: 添加了 ...

  8. jquery.jbox JBox-v2.3修改版

    原版jquery.jbox是个不错的jquery扩展,使用简单,功能很多.可惜的是作者把javascript加密了,并且2011年以后就不再更新.如果项目中用到了新的jquery版本,甚至jbox就没 ...

  9. Codeforces Round #352 (Div. 2) A

    A. Summer Camp time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  10. elemetUi 组件--el-checkbox

    [需求]实现选择右边的