zoukankan      html  css  js  c++  java
  • spring data jpa指定联合索引

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

    import lombok.AllArgsConstructor;
    import lombok.Getter;
    import lombok.NoArgsConstructor;
    import lombok.Setter;
    
    import javax.persistence.*;
    
    /**
     * 产品表
     *
     * @author wulinfeng
     * @since 2019/12/13
     */
    @Entity
    @Table(name = "t_product", uniqueConstraints = @UniqueConstraint(columnNames = {"orderId", "productId"}))
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    public class ProductItem {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;
    
        // 订单Id
        @Column(nullable = false, length = 32)
        private String orderId;
    
        // 受理产品编码
        @Column(length = 32)
        private String productId;
    
        // 产品名称
        @Column(length = 32)
        private String productName;
    
        // 时间戳
        @Column(length = 13)
        private Long 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)
  • 相关阅读:
    Python学习(18)——函数
    Python学习(17)——推导式
    labview隐藏任务栏
    labview下载地址
    labview出现系统998错误
    油猴使用指南
    无法执行该VI,必须使用LabVIEW完整版开发系统才可以解决该错误
    修改远程桌面连接默认端口
    labview使用了报表模块,在生成exe时需要添加以下内容,否则打包后不能开启excel功能
    腾讯通二次开发接口
  • 原文地址:https://www.cnblogs.com/wuxun1997/p/12101952.html
Copyright © 2011-2022 走看看