Java – learn about mappedby annotation in Hibernate

I try to understand the mappedby attribute of @ onetomany annotation in JPA I created the following example where the customer has an order list:

@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;
}

Now, when I use hibernate to generate tables, I see that hibernate has only created two tables:

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

In addition, if I try to save a customer and some orders, I see the following DML statement generated by Hibernate:

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=?

Why does hibernate try to insert and update TBL_ Record in order instead of just running an insert query?

Now, if I delete the mappedby attribute and try to generate these tables, this time I see three tables:

Hibernate: create table Customer (id number(10,primary key (id))
Hibernate: create table Customer_TBL_ORDER (Customer_id number(10,orders_id number(10,primary key (Customer_id,orders_index))
Hibernate: create table TBL_ORDER (id number(10,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

Why does hibernate create an additional table in this case? How is the mappedby property controlled? Why are we at customer_ TBL_ Orders in the order table_ Is there an additional unique key constraint on the ID column?

Now if I try to save a customer and its order, I get the following DML operation:

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

Why do we have no additional update queries in this case compared to the case where the mappedby attribute has been declared?

Solution

This is normal

Using mappedby, you directly tell hibernate / JPA that a table has a relationship, so it is stored as a column of the table

No, the relationship is external. Hibernate / JPA needs to create another table to store the relationship

Example:

There are several answers to a stack overflow question The answer consists of a single question

In simple JDBC, you will create two tables:

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

Where Question_ ID is the foreign key reference question Question_ ID.

For another case, I don't have a real case, because there are many to a unique constraint almost every time (for example, a question may have several answers, and the answer may actually have several questions, but any question only appears once)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>