jpa mysql索引_spring data jpa指定联合索引

如何,现在我的表里使用订单ID和产品ID作为唯一索引,那么需要在定义表实体类时在@Table中指定UniqueConstraint:

importlombok.AllArgsConstructor;importlombok.Getter;importlombok.NoArgsConstructor;importlombok.Setter;import javax.persistence.*;/*** 产品表

*

*@authorwulinfeng

*@since2019/12/13*/@Entity

@Table(name= "t_product", uniqueConstraints = @UniqueConstraint(columnNames = {"orderId", "productId"}))

@Getter

@Setter

@AllArgsConstructor

@NoArgsConstructorpublic classProductItem {

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)privateLong id;//订单Id

@Column(nullable = false, length = 32)privateString orderId;//受理产品编码

@Column(length = 32)privateString productId;//产品名称

@Column(length = 32)privateString productName;//时间戳

@Column(length = 13)privateLong timestamp;

}

把原来的t_product表drop掉,重启spring boot,再看该表,自动加上唯一索引了:

mysql>show index from t_product;+-----------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |

+-----------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

| t_product | 0 | PRIMARY | 1 | id | A | 2 | NULL | NULL | | BTREE | | |

| t_product | 0 | UK1mvw2lcd07t4cuicl4awfbgkw | 1 | order_id | A | 2 | NULL | NULL | | BTREE | | |

| t_product | 0 | UK1mvw2lcd07t4cuicl4awfbgkw | 2 | product_id | A | 2 | NULL | NULL | YES | BTREE | | |

+-----------+------------+-----------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

3 rows in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值