💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 一对多保存 ~~~ Customer customer = new Customer(); customer.setCust_name("jack"); Linkman linkman = new Linkman(); linkman.setLink_name("秘书"); customer.getLinkmens().add(linkman); linkman.setCustomer(customer); entityManager.persist(customer); entityManager.persist(linkman); ~~~ ## 一对多级联保存 ~~~ @OneToMany(targetEntity = Linkman.class, mappedBy = "customer", cascade = CascadeType.PERSIST) //设置级联保存 private Set<Linkman> linkmens = new HashSet<>(); ~~~ 代码: ~~~ Customer customer = new Customer(); customer.setCust_name("jack"); Linkman linkman1 = new Linkman(); linkman1.setLink_name("小王"); Linkman linkman2 = new Linkman(); linkman2.setLink_name("小张"); customer.getLinkmens().add(linkman1); customer.getLinkmens().add(linkman2); linkman1.setCustomer(customer); linkman2.setCustomer(customer); entityManager.persist(customer); ~~~ ## 普通删除 ~~~ Linkman linkman = entityManager.find(Linkman.class, 7L); entityManager.remove(linkman); ~~~ ## 级联删除 ~~~ @OneToMany(targetEntity = Linkman.class, mappedBy = "customer", cascade = CascadeType.REMOVE) //设置级联删除 private Set<Linkman> linkmens = new HashSet<>(); ~~~ 当删除customer的时候,linkman关联的记录也被删除了. ~~~ Customer customer = entityManager.find(Customer.class, 6L); entityManager.remove(customer); ~~~