一尘不染

了解Hibernate中的mappingBy批注

hibernate

我试图了解JPA mappedBy@OneToMany注释的属性。我在下面的示例中创建了一个客户具有订单列表的示例:

@Entity
public class Customer {
   @Id @GeneratedValue public Integer getId() { return id; }
   public void setId(Integer id) { this.id = id; }
   private Integer id;

   @OneToMany(mappedBy="customer")
   @OrderColumn(name="orders_index")
   public List<Order> getOrders() { return orders; }
   public void setOrders(List<Order> orders) { this.orders = orders; }
   private List<Order> orders;
}

@Entity
@Table(name="TBL_ORDER")
public class Order {
   @Id @GeneratedValue public Integer getId() { return id; }
   public void setId(Integer id) { this.id = id; }
   private Integer id;

   public int getOrderNumber() { return orderNumber; }
   public void setOrderNumber(int orderNumber) { this.orderNumber = orderNumber; }
   private int orderNumber;

   @ManyToOne
   public Customer getCustomer() { return customer; }
   public void setCustomer(Customer customer) { this.customer = customer; }
   private Customer customer;
}

现在,当我使用Hibernate生成表时,我看到Hibernate仅创建了2个表:

Hibernate: create table Customer (id number(10,0) not null, primary key (id))
Hibernate: create table TBL_ORDER (id number(10,0) not null, orderNumber number(10,0) not null, customer_id number(10,0), orders_index number(10,0), primary key (id))
Hibernate: alter table TBL_ORDER add constraint FK_nt24krtgqwcsynosqgk4jkvfv foreign key (customer_id) references Customer

另外,如果我尝试保存客户和一些订单,我会在下面看到由Hibernate生成的DML语句:

Hibernate: insert into Customer (id) values (?)
Hibernate: insert into TBL_ORDER (customer_id, orderNumber, id) values (?, ?, ?)
Hibernate: update TBL_ORDER set orders_index=? where id=?

为什么Hibernate试图在TBL_ORDER中插入和更新记录,而不仅仅是运行单个插入查询?

现在,如果我删除mapledBy属性并尝试生成表,这次将看到3个表:

Hibernate: create table Customer (id number(10,0) not null, primary key (id))
Hibernate: create table Customer_TBL_ORDER (Customer_id number(10,0) not null, orders_id number(10,0) not null, orders_index number(10,0) not null, primary key (Customer_id, orders_index))
Hibernate: create table TBL_ORDER (id number(10,0) not null, orderNumber number(10,0) not null, customer_id number(10,0), primary key (id))
Hibernate: alter table Customer_TBL_ORDER add constraint UK_sw94jktvh72tripj876s31052  unique (orders_id)
Hibernate: alter table Customer_TBL_ORDER add constraint FK_sw94jktvh72tripj876s31052 foreign key (orders_id) references TBL_ORDER
Hibernate: alter table Customer_TBL_ORDER add constraint FK_f03up2h945cg0dcbo2pdb1d3c foreign key (Customer_id) references Customer
Hibernate: alter table TBL_ORDER add constraint FK_nt24krtgqwcsynosqgk4jkvfv foreign key (customer_id) references Customer

在这种情况下,为什么hibernate会创建一个附加表?mappingBy属性如何控制它?为什么我们在Customer_TBL_ORDER表的orders_id列上有一个附加的唯一键约束?

现在,如果我尝试保存客户及其订单,我将获得以下DML操作:

Hibernate: insert into Customer (id) values (?)
Hibernate: insert into TBL_ORDER (customer_id, orderNumber, id) values (?, ?, ?)
Hibernate: insert into Customer_TBL_ORDER (Customer_id, orders_index, orders_id) values (?, ?, ?)

与我声明了appedBy属性的情况相比,为什么在这种情况下我们没有额外的更新查询?


阅读 226

收藏
2020-06-20

共1个答案

一尘不染

那很正常

使用mappedBy,您可以直接告诉Hibernate / JPA一个表拥有该关系,因此将其存储为该表的一列。

如果没有,则该关系是外部的,并且Hibernate / JPA需要创建另一个表来存储该关系。

例:

  • 一个stackoverflow Question有几个Answer
  • 一个Answer人只有一个人拥有Question

在纯JDBC中,您将创建两个表:

Questions(Question_ID, ...);
Answers(Answer_ID, Question_ID, ...);

Question_ID外键引用在哪里Question.Question_ID

至于另一种情况,我没有一个真实的案例,因为几乎每一次 都有唯一的约束 (例如:a
Question可能有多个Answer,并且Answer可能在物理上有多个Question,但对于任何一个都只出现一次Question)。

2020-06-20