-1

描述

我有一个系统,我可以在其中添加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;

}

问题

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

4

1 回答 1

0

您必须将 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

于 2020-04-29T11:58:28.233 回答