双向一对多和双向多对一是一样的,就是两方都维护对方嘛
package com.yjj.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* author
*/
@Entity
@Table(name = "t_product")
public class Product {
@Id
@GeneratedValue
private Long id;
private String name;
// 多对一:optional=false表示外键type_id不能为空
@ManyToOne(optional = true)
@JoinColumn(name = "type_id")
private ProductType type;
public Product() {
}
public Product(String name, ProductType type) {
this.name = name;
this.type = type;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ProductType getType() {
return type;
}
public void setType(ProductType type) {
this.type = type;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", type=" + type +
'}';
}
}
package com.yjj.entity;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "t_product_type")
public class ProductType {
@Id
@GeneratedValue
private Long id;
private String name;
@OneToMany
@JoinColumn(name="type_id")
private List<Product> list = new ArrayList<Product>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Product> getList() {
return list;
}
public void setList(List<Product> list) {
this.list = list;
}
@Override
public String toString() {
return "ProductType{" +
"id=" + id +
", name='" + name + '\'' +
", list=" + list +
'}';
}
}
ProductType type=new ProductType();
type.setName("数码");
Product product=new Product();
product.setName("手机");
product.setType(type);
Product product1=new Product();
product1.setName("电脑");
product1.setType(type);
type.getList().add(product);
type.getList().add(product1);
entityManager.persist(type);
entityManager.persist(product);
entityManager.persist(product1);
先保存一的一方,再保存多的一方
Hibernate:
insert
into
t_product_type
(name)
values
(?)
Hibernate:
insert
into
t_product
(name, type_id)
values
(?, ?)
Hibernate:
insert
into
t_product
(name, type_id)
values
(?, ?)
Hibernate:
update
t_product
set
type_id=?
where
id=?
Hibernate:
update
t_product
set
type_id=?
where
id=?
换换位置
entityManager.persist(product);
entityManager.persist(product1);
entityManager.persist(type);
再看输出sql
Hibernate:
insert
into
t_product
(name, type_id)
values
(?, ?)
Hibernate:
insert
into
t_product
(name, type_id)
values
(?, ?)
Hibernate:
insert
into
t_product_type
(name)
values
(?)
Hibernate:
update
t_product
set
name=?,
type_id=?
where
id=?
Hibernate:
update
t_product
set
name=?,
type_id=?
where
id=?
Hibernate:
update
t_product
set
type_id=?
where
id=?
Hibernate:
update
t_product
set
type_id=?
where
id=?
多了两个update,所以知道怎么做了嘛?嗯?小傻瓜~
我们还可以用mappedBy注解,让一的一方来维护关系。
@OneToMany(mappedBy="user")
private List<Order> orders = new ArrayList<>();
但是使用了mapperBy注解后就不能用JoinColumn注解了