【问题标题】:Indexing for key which belongs to Primary key of other table索引属于其他表主键的键
【发布时间】:2021-08-26 01:10:20
【问题描述】:

我有两个表作为表

@Entity
 @Table(name = "product", uniqueConstraints=@UniqueConstraint(columnNames={"product_idn"}))
public class Product implements Serializable {
    private static final long serialVersionUID = 21409635044L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String product_idn;
}

另一个表

@Entity
@Table(name = "storage")
public class Storage implements Serializable {
    private static final long serialVersionUID = -67165579239L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", nullable = false)
    @JsonBackReference
    private Product product;
}

现在我正在从 storage 表中获取记录。是否应该在存储表的 product_id 上创建索引(非唯一)?我必须根据产品获取存储数量/根据产品获取存储。

【问题讨论】:

    标签: mysql hibernate jpa spring-data-jpa


    【解决方案1】:

    只需像下面这样添加 @Index 注释,它应该可以正常工作。

    @Entity
    @Table(name = "storage", indexes = @Index(columnList = "product_id",name = "storage_productId_index"))
    public class Storage implements Serializable 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      • 2013-03-27
      • 2021-05-04
      • 2014-05-05
      • 1970-01-01
      相关资源
      最近更新 更多