JPA--SpringDataJPA一对多关联、级联操作 短命女 2021-08-13 20:54 412阅读 0赞 ### 实体类 ### package pers.zhang.entity; /** * @author zhang * @date 2019/12/15 - 22:10 */ import javax.persistence.*; import java.util.HashSet; import java.util.Set; /** * 1.实体类和表的映射关系 * @Entity * @Table * 2.类中属性和表中字段的映射关系 * @Id * @GeneratedValue * @Colum */ @Entity //声明实体类 @Table(name="cst_customer") //建立实体类和表的映射关系 public class Customer { @Id//声明当前私有属性为主键 @GeneratedValue(strategy= GenerationType.IDENTITY) //配置主键的生成策略 @Column(name="cust_id") //指定和表中cust_id字段的映射关系 private Long custId; @Column(name="cust_name") //指定和表中cust_name字段的映射关系 private String custName; @Column(name="cust_source")//指定和表中cust_source字段的映射关系 private String custSource; @Column(name="cust_industry")//指定和表中cust_industry字段的映射关系 private String custIndustry; @Column(name="cust_level")//指定和表中cust_level字段的映射关系 private String custLevel; @Column(name="cust_address")//指定和表中cust_address字段的映射关系 private String custAddress; @Column(name="cust_phone")//指定和表中cust_phone字段的映射关系 private String custPhone; //配置客户和联系人之间的关系(一对多) /* 1.声明关系: 配置一对多关系:@OneToMany:targetEntity(对方对象的字节码) 2.配置外键(中间表) name:外键字段的名称 referencedColumnName:参照的主表的主键字段名称 在客户实体类上(一的一方)添加了外键的配置,所以对于客户而言,也具备了维护外键的作用 实际情况下,只需要一方维护关系即可 mappedBy表示:放弃外键维护,由对方维护,取值为对方配置关系的属性名称 级联:cascade CascadeType.ALL:所有 MERGE:更新 PERSIST:保存 REMOVE:删除 */ // @OneToMany(targetEntity = LinkMan.class) // @JoinColumn(name = "lkm_cust_id", referencedColumnName = "cust_id") @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL) private Set<LinkMan> linkMans = new HashSet<>(); public Set<LinkMan> getLinkMans() { return linkMans; } public void setLinkMans(Set<LinkMan> linkMans) { this.linkMans = linkMans; } public Long getCustId() { return custId; } public void setCustId(Long custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustIndustry() { return custIndustry; } public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } @Override public String toString() { return "Customer{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custIndustry='" + custIndustry + '\'' + ", custLevel='" + custLevel + '\'' + ", custAddress='" + custAddress + '\'' + ", custPhone='" + custPhone + '\'' + '}'; } } package pers.zhang.entity; import org.springframework.test.context.ContextConfiguration; import sun.awt.SunHints; import javax.naming.Name; import javax.persistence.*; /** * @author zhang * @date 2019/12/17 - 20:23 */ @Entity @Table(name = "cst_linkman") public class LinkMan { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "lkm_id") private Long lkmId;//联系人编号 @Column(name = "lkm_name") private String lkmName;//联系人姓名 @Column(name = "lkm_gender") private String lkmGender;//联系人性别 @Column(name = "lkm_phone") private String lkmPhone;//联系人办公电话 @Column(name = "lkm_mobile") private String lkmMobile;//联系人手机 @Column(name = "lkm_email") private String lkmEmail;//联系人E-mail @Column(name = "lkm_position") private String lkmPosition;//联系人地址 @Column(name = "lkm_memo") private String lkmMemo;//联系人备注 //配置联系人到客户的多对一关系 /* @ManyToOne:配置多对一关系 targetEntity:对方的实体类额字节码 @JoinColumn:配置外键(中间表) name:外键字段名 referencedColumnName:外键引用的主表主键字段名 */ @ManyToOne(targetEntity = Customer.class) @JoinColumn(name = "lkm_cust_id", referencedColumnName = "cust_id") private Customer customer; public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public Long getLkmId() { return lkmId; } public void setLkmId(Long lkmId) { this.lkmId = lkmId; } public String getLkmName() { return lkmName; } public void setLkmName(String lkmName) { this.lkmName = lkmName; } public String getLkmGender() { return lkmGender; } public void setLkmGender(String lkmGender) { this.lkmGender = lkmGender; } public String getLkmPhone() { return lkmPhone; } public void setLkmPhone(String lkmPhone) { this.lkmPhone = lkmPhone; } public String getLkmMobile() { return lkmMobile; } public void setLkmMobile(String lkmMobile) { this.lkmMobile = lkmMobile; } public String getLkmEmail() { return lkmEmail; } public void setLkmEmail(String lkmEmail) { this.lkmEmail = lkmEmail; } public String getLkmPosition() { return lkmPosition; } public void setLkmPosition(String lkmPosition) { this.lkmPosition = lkmPosition; } public String getLkmMemo() { return lkmMemo; } public void setLkmMemo(String lkmMemo) { this.lkmMemo = lkmMemo; } } ### Dao接口 ### package pers.zhang.dao; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import pers.zhang.entity.Customer; /** * @author zhang * @date 2019/12/17 - 20:22 */ public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> { } package pers.zhang.dao; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import pers.zhang.entity.LinkMan; /** * @author zhang * @date 2019/12/17 - 20:36 */ public interface LinkManDao extends JpaRepository<LinkMan, Long>, JpaSpecificationExecutor<LinkMan> { } ### 测试类 ### package pers.zhang; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import pers.zhang.dao.CustomerDao; import pers.zhang.dao.LinkManDao; import pers.zhang.entity.Customer; import pers.zhang.entity.LinkMan; /** * @author zhang * @date 2019/12/17 - 20:23 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class myTest { @Autowired private CustomerDao customerDao; @Autowired private LinkManDao linkManDao; /* 保存一个客户,一个联系人 */ @Test @Transactional @Rollback(false) public void testAdd(){ Customer customer = new Customer(); customer.setCustName("百度百度"); LinkMan linkMan = new LinkMan(); linkMan.setLkmName("小李"); //客户对外键进行维护 customer.getLinkMans().add(linkMan); linkManDao.save(linkMan); customerDao.save(customer); /* Hibernate: insert into cst_linkman (lkm_cust_id, lkm_email, lkm_gender, lkm_memo, lkm_mobile, lkm_name, lkm_phone, lkm_position) values (?, ?, ?, ?, ?, ?, ?, ?) Hibernate: insert into cst_customer (cust_address, cust_industry, cust_level, cust_name, cust_phone, cust_source) values (?, ?, ?, ?, ?, ?) Hibernate: update cst_linkman set lkm_cust_id=? where lkm_id=? */ } @Test @Transactional @Rollback(false) public void testAdd2(){ Customer customer = new Customer(); customer.setCustName("育碧"); LinkMan linkMan = new LinkMan(); linkMan.setLkmName("歌玲"); //联系人维护关系 linkMan.setCustomer(customer); linkManDao.save(linkMan); customerDao.save(customer); /* Hibernate: insert into cst_linkman (lkm_cust_id, lkm_email, lkm_gender, lkm_memo, lkm_mobile, lkm_name, lkm_phone, lkm_position) values (?, ?, ?, ?, ?, ?, ?, ?) Hibernate: insert into cst_customer (cust_address, cust_industry, cust_level, cust_name, cust_phone, cust_source) values (?, ?, ?, ?, ?, ?) Hibernate: update cst_linkman set lkm_cust_id=?, lkm_email=?, lkm_gender=?, lkm_memo=?, lkm_mobile=?, lkm_name=?, lkm_phone=?, lkm_position=? where lkm_id=? */ } /* 级联:操作一个对象的同时操作它的关联对象 级联操作: 1. 需要区分操作主体 2. 需要在操作主体的实体类上,添加级联属性(需要添加到多表映射关系的注解上) 3. cascade(配置级联) 级联添加: 示例:保存一个客户的同时保存联系人 级联删除: 示例:删除一个客户的同时删除对应的所有联系人 */ /* 级联添加:保存一个客户的同时保存联系人 */ @Test @Transactional @Rollback(false) public void testCascadeAdd(){ Customer customer = new Customer(); customer.setCustName("任天堂"); LinkMan linkMan1 = new LinkMan(); linkMan1.setLkmName("AAA"); LinkMan linkMan2 = new LinkMan(); linkMan2.setLkmName("BBB"); //联系人维护关系 linkMan1.setCustomer(customer); linkMan2.setCustomer(customer); customer.getLinkMans().add(linkMan1); customer.getLinkMans().add(linkMan2); customerDao.save(customer); /* Hibernate: insert into cst_customer (cust_address, cust_industry, cust_level, cust_name, cust_phone, cust_source) values (?, ?, ?, ?, ?, ?) Hibernate: insert into cst_linkman (lkm_cust_id, lkm_email, lkm_gender, lkm_memo, lkm_mobile, lkm_name, lkm_phone, lkm_position) values (?, ?, ?, ?, ?, ?, ?, ?) Hibernate: insert into cst_linkman (lkm_cust_id, lkm_email, lkm_gender, lkm_memo, lkm_mobile, lkm_name, lkm_phone, lkm_position) values (?, ?, ?, ?, ?, ?, ?, ?) */ } /* 删除主表数据: 1. 有从表数据: 1.1:在默认情况下,它会把外键字段置为null,然后删除主表数据。如果在数据库的表结构上,外键字段有非空约束,默认情况就会报错。 1.2:如果配置了放弃维护关联关系,则不能删除(与外键字段是否允许为null没有关系),因为在删除时,它根本不会去更新从表的外键字段。 1.3:如果还想删除,使用级联删除引用 2. 没有从表数据: 随便删~~~ */ /* 级联删除:删除客户的同时,删除对应的所有联系人 */ @Test @Transactional @Rollback(false) public void testCascadeDelete(){ Customer customer = customerDao.findOne(14L); customerDao.delete(customer); /* Hibernate: select customer0_.cust_id as cust_id1_0_0_, customer0_.cust_address as cust_add2_0_0_, customer0_.cust_industry as cust_ind3_0_0_, customer0_.cust_level as cust_lev4_0_0_, customer0_.cust_name as cust_nam5_0_0_, customer0_.cust_phone as cust_pho6_0_0_, customer0_.cust_source as cust_sou7_0_0_ from cst_customer customer0_ where customer0_.cust_id=? Hibernate: select linkmans0_.lkm_cust_id as lkm_cust9_1_0_, linkmans0_.lkm_id as lkm_id1_1_0_, linkmans0_.lkm_id as lkm_id1_1_1_, linkmans0_.lkm_cust_id as lkm_cust9_1_1_, linkmans0_.lkm_email as lkm_emai2_1_1_, linkmans0_.lkm_gender as lkm_gend3_1_1_, linkmans0_.lkm_memo as lkm_memo4_1_1_, linkmans0_.lkm_mobile as lkm_mobi5_1_1_, linkmans0_.lkm_name as lkm_name6_1_1_, linkmans0_.lkm_phone as lkm_phon7_1_1_, linkmans0_.lkm_position as lkm_posi8_1_1_ from cst_linkman linkmans0_ where linkmans0_.lkm_cust_id=? Hibernate: delete from cst_linkman where lkm_id=? Hibernate: delete from cst_linkman where lkm_id=? Hibernate: delete from cst_customer where cust_id=? */ } }
还没有评论,来说两句吧...