一对多映射会创建3个表

时间:2018-08-06 03:49:31

标签: hibernate spring-boot jpa h2

嗨,我正在使用h2数据库和hibernate进行一对多映射。我的spring boot应用程序使用实体类创建表。我发现有3个表正在创建。因此,只有在多对多的情况下,我们才需要第三个表来维护两者之间的关系。就我而言,即使是一对多,我也可以看到正在创建3个表。有人可以阐明这一点吗?

@ApiModel(description = "Class representing a Order placed by custormer")
@Entity
 @Table(name = "order_taken")
  public class Order {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "order_id")
private int orderId;

@Column(name = "quantity")
private int quantity;


@OneToMany(fetch = FetchType.LAZY ,cascade = CascadeType.ALL)
private Set<Item> items;


 @ApiModel(description = "Class representing all coffee varieties in our 
   Restaurant")
  @Entity
     @Table(name="items")
     public class Item {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="item_id")
private int itemId;

@Column(name="item_name")
private String itemName;

@Column(name="price")
private double price;

对于上述实体,有3个表item,orders_taken和orders_taken_item。

1 个答案:

答案 0 :(得分:2)

如果您不想创建关系表,则需要指定@JoinColum

将其添加到Order表中:

@OneToMany(fetch = FetchType.LAZY ,cascade = CascadeType.ALL)
@JoinColumn(name = "order_id")
private Set<Item> items;