<properties> <spring.version>5.2.5.RELEASE</spring.version> <hibernate.version>5.4.10.Final</hibernate.version> <slf4j.version>1.7.30</slf4j.version> <log4j.version>2.12.1</log4j.version> <druid.version>1.1.21</druid.version> <mysql.version>5.1.6</mysql.version> </properties> <dependencies> <!-- spring beg --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- spring對orm框架的支持包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- hibernate beg --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.1.2.Final</version> <exclusions> <exclusion> <artifactId>classmate</artifactId> <groupId>com.fasterxml</groupId> </exclusion> </exclusions> </dependency> <!-- hibernate end --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- spring data jpa 的座標 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>2.2.6.RELEASE</version> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <!-- el beg 使用spring data jpa 必須引入 --> <dependency> <groupId>javax.el</groupId> <artifactId>javax.el-api</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.el</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" xmlns:contxt="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <!-- 配置實體類管理工廠 --> <bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.ytfs.entity"/> <property name="persistenceProvider"> <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/> </property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="true"/> <property name="database" value="MYSQL"/> <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/> <property name="generateDdl" value="false"/> </bean> </property> </bean> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 整合jpa --> <jpa:repositories base-package="com.ytfs.dao" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactoryBean"/> <!-- spring的包掃描 --> <context:component-scan base-package="com.ytfs"/> <!-- 整合數據源 --> <context:property-placeholder location="classpath:jdbcConfig.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="url" value="${jdbc.url}"/> </bean> </beans>
@OneToMany:
做用:創建一對多的關係映射
屬性:
targetEntityClass:指定多的多方的類的字節碼
mappedBy:指定從表實體類中引用主表對象的名稱。
cascade:指定要使用的級聯操做
fetch:指定是否採用延遲加載
orphanRemoval:是否使用孤兒刪除
@ManyToOne
做用:創建多對一的關係
屬性:
targetEntityClass:指定一的一方實體類字節碼
cascade:指定要使用的級聯操做
fetch:指定是否採用延遲加載
optional:關聯是否可選。若是設置爲false,則必須始終存在非空關係。
@JoinColumn
做用:用於定義主鍵字段和外鍵字段的對應關係。
屬性:
name:指定外鍵字段的名稱
referencedColumnName:指定引用主表的主鍵字段名稱
unique:是否惟一。默認值不惟一
nullable:是否容許爲空。默認值容許。
insertable:是否容許插入。默認值容許。
updatable:是否容許更新。默認值容許。
columnDefinition:列的定義信息。
package xyz.ytfs.entity; import org.springframework.test.context.ContextConfiguration; import javax.persistence.*; import java.util.HashSet; import java.util.Set; /** * @Classname Customer * @Description TODO(客戶的實體類) * @Date 2020/5/8 23:20 * @Created by ytfs */ @Entity @Table(name = "cst_customer") public class Customer { /** * CREATE TABLE `cst_linkman` ( * `lkm_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '聯繫人編號(主鍵)', * `lkm_name` varchar(16) DEFAULT NULL COMMENT '聯繫人姓名', * `lkm_gender` char(1) DEFAULT NULL COMMENT '聯繫人性別', * `lkm_phone` varchar(16) DEFAULT NULL COMMENT '聯繫人辦公電話', * `lkm_mobile` varchar(16) DEFAULT NULL COMMENT '聯繫人手機', * `lkm_email` varchar(64) DEFAULT NULL COMMENT '聯繫人郵箱', * `lkm_position` varchar(16) DEFAULT NULL COMMENT '聯繫人職位', * `lkm_memo` varchar(512) DEFAULT NULL COMMENT '聯繫人備註', * `lkm_cust_id` bigint(32) NOT NULL COMMENT '客戶id(外鍵)', * PRIMARY KEY (`lkm_id`), * KEY `FK_cst_linkman_lkm_cust_id` (`lkm_cust_id`), * CONSTRAINT `FK_cst_linkman_lkm_cust_id` FOREIGN KEY (`lkm_cust_id`) REFERENCES `cst_customer` (`cust_id`) ON DELETE NO ACTION ON UPDATE NO ACTION * ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "cust_id") private Long custId; @Column(name = "cust_name") private String custName; @Column(name = "cust_source") private String custSource; @Column(name = "cust_industry") private String custIndustry; @Column(name = "cust_level") private String custLevel; @Column(name = "cust_address") private String custAddress; @Column(name = "cust_phone") private String custPhone; /* 表達一對多的關係 */ /** * * 使用註解的形式配置多表關係 * 1.聲明關係 * @OneToMany : 配置一對多關係 * targetEntity :對方對象的字節碼對象 * 2.配置外鍵(中間表) * @JoinColumn : 配置外鍵 * name:外鍵字段名稱 * referencedColumnName:參照的主表的主鍵字段名稱 * * *在客戶實體類上(一的一方)添加了外鍵了配置,因此對於客戶而言,也具有了維護外鍵的做用 */ //@OneToMany(targetEntity = LinkMan.class) //@JoinColumn(name = "lkm_cust_id", referencedColumnName = "cust_id") /** * 放棄外鍵維護權 * mappedBy:對方配置關係的屬性名稱\ 這裏就是值得LinkMan中Customer對象得變量名稱 * cascade : 配置級聯(能夠配置到設置多表的映射關係的註解上) * CascadeType.all : 全部 * MERGE :更新 * PERSIST :保存 * REMOVE :刪除 * <p> * fetch : 配置關聯對象的加載方式 * EAGER :當即加載 * LAZY :延遲加載 */ @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private Set<LinkMan> linkMans = new HashSet<>(); 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; } public Set<LinkMan> getLinkMans() { return linkMans; } public void setLinkMans(Set<LinkMan> linkMans) { this.linkMans = linkMans; } @Override public String toString() { return "Customer{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custIndustry='" + custIndustry + '\'' + ", custLevel='" + custLevel + '\'' + ", custAddress='" + custAddress + '\'' + ", custPhone='" + custPhone + '\'' + '}'; } }
package xyz.ytfs.entity; import javax.persistence.*; /** * @Classname LinkMan * @Description TODO(聯繫人的實體類) * @Date 2020/5/8 23:16 * @Created by ytfs */ @Entity @Table(name = "cst_linkman") public class LinkMan { /** * CREATE TABLE `cst_customer` ( * `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客戶編號(主鍵)', * `cust_name` varchar(32) NOT NULL COMMENT '客戶名稱(公司名稱)', * `cust_source` varchar(32) DEFAULT NULL COMMENT '客戶信息來源', * `cust_industry` varchar(32) DEFAULT NULL COMMENT '客戶所屬行業', * `cust_level` varchar(32) DEFAULT NULL COMMENT '客戶級別', * `cust_address` varchar(128) DEFAULT NULL COMMENT '客戶聯繫地址', * `cust_phone` varchar(64) DEFAULT NULL COMMENT '客戶聯繫電話', * PRIMARY KEY (`cust_id`) * ) ENGINE=InnoDB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8; */ @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; @Column(name = "lkm_position") private String lkmPosition; @Column(name = "lkm_memo") private String lkmMemo; /* 表達多對一的關係 */ @ManyToOne(targetEntity = Customer.class) @JoinColumn(name = "lkm_cust_id", referencedColumnName = "cust_id") private 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; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } @Override public String toString() { return "LinkMan{" + "lkmId=" + lkmId + ", lkmName='" + lkmName + '\'' + ", lkmGender='" + lkmGender + '\'' + ", lkmPhone='" + lkmPhone + '\'' + ", lkmMobile='" + lkmMobile + '\'' + ", lkmEmail='" + lkmEmail + '\'' + ", lkmPosition='" + lkmPosition + '\'' + ", lkmMemo='" + lkmMemo + '\'' + '}'; } }
package xyz.ytfs.test; import org.hibernate.loader.plan.build.internal.returns.CollectionFetchableIndexAnyGraph; 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 xyz.ytfs.dao.ICustomerDao; import xyz.ytfs.dao.ILinkMan; import xyz.ytfs.entity.Customer; import xyz.ytfs.entity.LinkMan; import javax.transaction.Transactional; import javax.validation.constraints.AssertFalse; import java.util.List; import java.util.Optional; /** * @Classname JpaOnetoManyTest * @Description TODO(JPA多對一的測試類) * @Date 2020/5/9 0:10 * @Created by ytfs */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class JpaOnetoManyTest { @Autowired private ICustomerDao customerDao; @Autowired private ILinkMan linkManDao; @Test @Transactional //配置事務 @Rollback(false) //不自動回滾 public void testSave1() { Customer customer = new Customer(); customer.setCustName("雨聽風說"); LinkMan linkMan = new LinkMan(); linkMan.setLkmName("小鄧"); /* * 配置了客戶到聯繫人的關係 * 從客戶的角度上:發送兩條insert語句,發送一條更新語句更新數據庫(更新外鍵) * 因爲咱們配置了客戶到聯繫人的關係:客戶能夠對外鍵進行維護 */ customer.getLinkMans().add(linkMan); this.customerDao.save(customer); this.linkManDao.save(linkMan); } @Test @Transactional //配置事務 @Rollback(false) //不自動回滾 public void testSave2() { //建立一個客戶,建立一個聯繫人 Customer customer = new Customer(); customer.setCustName("百度"); LinkMan linkMan = new LinkMan(); linkMan.setLkmName("小李"); /** * 配置聯繫人到客戶的關係(多對一) * 只發送了兩條insert語句 * 因爲配置了聯繫人到客戶的映射關係(多對一) * * */ linkMan.setCustomer(customer); this.customerDao.save(customer); this.linkManDao.save(linkMan); } /** * 會有一條多餘的update語句 * * 因爲一的一方能夠維護外鍵:會發送update語句 * * 解決此問題:只須要在一的一方放棄維護權便可 */ @Test @Transactional //配置事務 @Rollback(false) //不自動回滾 public void testAdd2() { //建立一個客戶,建立一個聯繫人 Customer customer = new Customer(); customer.setCustName("百度"); LinkMan linkMan = new LinkMan(); linkMan.setLkmName("小李"); linkMan.setCustomer(customer);//因爲配置了多的一方到一的一方的關聯關係(當保存的時候,就已經對外鍵賦值) customer.getLinkMans().add(linkMan);//因爲配置了一的一方到多的一方的關聯關係(發送一條update語句) this.customerDao.save(customer); this.linkManDao.save(linkMan); } /** * 級聯添加:保存一個客戶的同時,保存客戶的全部聯繫人 * 須要在操做主體(這裏就是Customer)的實體類上,配置casacde屬性 */ @Test @Transactional @Rollback(false) public void testSaveCasacde() { Customer customer = new Customer(); customer.setCustName("雨聽風說1"); LinkMan linkMan = new LinkMan(); linkMan.setLkmName("小李1"); /* 表達關係 */ customer.getLinkMans().add(linkMan); linkMan.setCustomer(customer); /* 級聯操做得時候在保存客戶得同時會自動保存客戶對應得聯繫人,可是須要在一的一方配置級聯操做 */ this.customerDao.save(customer); } @Test @Transactional //配置事務 @Rollback(false) //不自動回滾 public void testCascadeRemove() { //1.查詢1號客戶 Customer customer = customerDao.getOne(1l); //2.刪除1號客戶 System.out.println(customer); customerDao.delete(customer); } }
package xyz.ytfs.test; import org.aspectj.weaver.ast.Var; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import xyz.ytfs.dao.ICustomerDao; import xyz.ytfs.dao.ILinkMan; import xyz.ytfs.entity.Customer; import xyz.ytfs.entity.LinkMan; import java.util.Optional; import java.util.Set; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class ObjectQueryTest { @Autowired private ICustomerDao customerDao; @Autowired private ILinkMan linkManDao; //could not initialize proxy - no Session //測試對象導航查詢(查詢一個對象的時候,經過此對象查詢全部的關聯對象) @Test @Transactional // 解決在java代碼中的no session問題 public void testQuery1() { //查詢id爲1的客戶 Customer customer = this.customerDao.getOne(1L); //對象導航查詢,此客戶下的全部聯繫人 Set<LinkMan> linkMans = customer.getLinkMans(); linkMans.forEach(System.out::println); } /** * 對象導航查詢: * 默認使用的是延遲加載的形式查詢的 * 調用get方法並不會當即發送查詢,而是在使用關聯對象的時候纔會差和訊 * 延遲加載! * 修改配置,將延遲加載改成當即加載 * fetch,須要配置到多表映射關係的註解上 * */ @Test @Transactional // 解決在java代碼中的no session問題 public void testQuery2() { //查詢id爲1的客戶 Customer customer = this.customerDao.getOne(1L); //對象導航查詢,此客戶下的全部聯繫人 Set<LinkMan> linkMans = customer.getLinkMans(); linkMans.forEach(System.out::println); } /** * 從聯繫人對象導航查詢他的所屬客戶 * * 默認 : 當即加載 * 延遲加載: * */ @Test @Transactional // 解決在java代碼中的no session問題 public void testQuery3() { //對象導航查詢所屬的客戶 LinkMan linkMan = this.linkManDao.getOne(1L); Customer customer = linkMan.getCustomer(); System.out.println("customer = " + customer); } }