1.课程介绍
1. 单向一对多(掌握)
2. 集合映射(掌握)
3. 双向一对多/双向多对一(掌握)
4. 单向多对多(掌握)
5. 双向多对多(掌握)
6. 一对一(掌握,用得少)
一.单向一对多
单向一对多很少使用,性能差
配置一对多
@Entity
@Table(name = "productDir")
public class ProductDir {
@Id
@GeneratedValue
private long id;
private String name;
//迫切加载
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "dir_id")
private List<Product> products = new ArrayList<>();
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
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;
}
}
先保存多方在保存一方和先保存一方在保存多方一样都是5条,效率低
EntityManager entityManager = JpaUtils.getEntityManager();
//瞬时
entityManager.getTransaction().begin();
//持久
entityManager.persist(p1);
entityManager.persist(p2);
entityManager.persist(dir);
//游离
entityManager.getTransaction().commit();
entityManager.close();
判断是否有值
@Test
public void testQuery1() throws Exception{
EntityManager entityManager = JpaUtils.getEntityManager();
ProductDir dir = entityManager.find(ProductDir.class, 1L);
//打印产品类型
System.out.println(dir);
System.out.println(dir.getProducts());
//判断是否有无该产品
if (dir.getProducts().size()==0){
System.out.println("当前产品没有类型");
}else {
System.out.println("有该产品类型");
}
entityManager.close();
}
二.集合映射
两种映射特点
Set:不能重复,集合里面的元素没有顺序
List:可以重复,集合里面的元素有顺序
配置集合的时候必须配置接口
一般都是使用Set接口,只有组合关系使用List接口
@Test
public void testQuery() throws Exception{
EntityManager entityManager = JpaUtils.getEntityManager();
ProductDir dir = entityManager.find(ProductDir.class, 1L);
//org.hibernate.collection.internal.PersistentBag和ArrayList是同级,和List是父子关系
//org.hibernate.collection.internal.PersistentSet和HashSet是同级,和Set是父子关系
System.out.println(dir.getProducts().getClass());
}
双向一对多和双向多对一(掌握)
双向就是两方都要配置相关配置
双向一对多
//多方
@ManyToOne
//加入列
@JoinColumn(name = "dir_id")
private ProductDir dir;
@Id
@GeneratedValue
private long id;
private String name;
//懒加载
@OneToMany(cascade = CascadeType.ALL,orphanRemoval = true,mappedBy = "dir",fetch = FetchType.LAZY)
// @JoinColumn(name = "dir_id")
private List<Product> products = new ArrayList<>();
双向多对多
@Id
@GeneratedValue
private Long id;
private String name;
//双向多对多,级联方式所有
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="user_role",joinColumns = @JoinColumn(name="user_id"),
inverseJoinColumns =@JoinColumn(name="role_id") )
@Id
@GeneratedValue
private Long id;
private String name;
//双向多对多,,级联配置移除,,危险操作
@ManyToMany(cascade = CascadeType.REMOVE)
@JoinTable(name="user_role",joinColumns = @JoinColumn(name="role_id"),
inverseJoinColumns =@JoinColumn(name="user_id") )
级联操作(危险)
级联保存 PERSIST
级联删除 REMOVE
@ManyToMany(cascade = CascadeType.REMOVE)
强级联 ALL
@ManyToMany(cascade = CascadeType.ALL)
孤儿删除 orphanRemoval = true (从一方解除关系,让多方外键置为空)
@OneToMany(cascade = CascadeType.ALL,orphanRemoval = true,mappedBy = "dir",fetch = FetchType.LAZY)
双方都配置了就是删库跑路了
单向多对多(掌握)
多方
@Id
@GeneratedValue
private long id;
private String name;
private BigDecimal price;
//多方
@ManyToOne
//加入列
@JoinColumn(name = "dir_id")
@Id
@GeneratedValue
private long id;
private String name;
//懒加载
@OneToMany(cascade = CascadeType.ALL,orphanRemoval = true,mappedBy = "dir",fetch = FetchType.LAZY)
// @JoinColumn(name = "dir_id")
private List<Product> products = new ArrayList<>();
双向多对多(掌握)
两边都配置
@Id
@GeneratedValue
private Long id;
private String name;
//双向多对多,级联方式所有
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name="user_role",joinColumns = @JoinColumn(name="user_id"),
inverseJoinColumns =@JoinColumn(name="role_id") )
@Id
@GeneratedValue
private Long id;
private String name;
//双向多对多,,级联配置移除,,危险操作
@ManyToMany(cascade = CascadeType.REMOVE)
@JoinTable(name="user_role",joinColumns = @JoinColumn(name="role_id"),
inverseJoinColumns =@JoinColumn(name="user_id") )
private Set<User> roles=new HashSet<>();
单向一对一(了解)不用