【问题标题】:How to store amount of objects in Spring data如何在 Spring 数据中存储对象的数量
【发布时间】:2020-08-13 12:07:11
【问题描述】:

说明

我有一个系统,我可以在其中添加 Component 并将其保存到 DB(Mysql)。之后可以创建Product,其中包含不同数量的Components。 据我了解,表格应该类似于component_id|product_id|amount_of_component

组件类:

@Entity
@Table(name = "component")
public class Component {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
}

产品类别:

@Entity
@Table(name = "product")
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;

  //something should be here
    private Component component;

}

问题

我应该使用哪个注释或集合来创建实体之间的这种关系?

【问题讨论】:

    标签: java spring jpa spring-data-jpa spring-data


    【解决方案1】:

    您必须将 OneToMany 与 Set 或 List 结合使用。这是一个例子:

    @OneToMany
    @JoinColumn("product_id")
    private Set<Component> component;
    

    请阅读有关集合映射的 Hibernate 文档:https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#collections

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 2011-10-22
      • 2020-09-30
      • 2010-10-15
      • 2021-12-11
      • 1970-01-01
      相关资源
      最近更新 更多