1. 程式人生 > >springdatajpa多表一對多單向關聯

springdatajpa多表一對多單向關聯

7.多表關係—>一對多查詢

7.1實體類配置–>一的一方:需要配置從表的外來鍵
package com.xcl.domain;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "com_company")
public class Company {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "com_id"
) private Long comId; @Column(name = "com_name") private String comName; @OneToMany(targetEntity = Employee.class,cascade = {CascadeType.PERSIST})//CascadeType.PERSIST級聯新增 @JoinColumn(name = "com_emp_id",referencedColumnName = "com_id") private Set<Employee> employees =
new HashSet<Employee>(0); //TODO... //gettee()... //setter()... //toString()... }
7.2實體類配置:多的一方–>正常配置,無需加入多餘的資訊
package com.xcl.domain;
import javax.persistence.*;
@Entity
@Table(name = "emp_employee")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.
IDENTITY) @Column(name = "emp_id") private Long empId; @Column(name = "emp_name") private String empName; }
7.3符合規範的Dao介面
package com.xcl.dao;
import com.xcl.domain.Company;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface CompanyDao extends JpaRepository<Company,Long>,JpaSpecificationExecutor<Company> {
}
package com.xcl.dao;
import com.xcl.domain.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
public interface EmployeeDao extends JpaRepository<Employee,Long>,JpaSpecificationExecutor<Employee> {
}
7.4測試:多表關聯一對多單向繫結操作
package com.xcl;
import com.xcl.dao.CompanyDao;
import com.xcl.dao.EmployeeDao;
import com.xcl.domain.Company;
import com.xcl.domain.Employee;
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 javax.persistence.Table;
import java.util.HashSet;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beans.xml")
public class OneToManyTest {
    @Autowired
    private CompanyDao companyDao;
    @Autowired
    private EmployeeDao employeeDao;
    /**
     * 一對多關係新增
     */
    @Test
    @Transactional  //配置事務
    @Rollback(false)    //不回滾
    public void test01() {
        //建立公司
        Company company = new Company();
        company.setComName("千度");
        //建立員工
        Employee employee01 = new Employee();
        employee01.setEmpName("小李");
        Employee employee02 = new Employee();
        employee02.setEmpName("老李");
        //新增員工到公司集合
        HashSet<Employee> empList = new HashSet<Employee>();
        empList.add(employee01);
        empList.add(employee02);
        company.setEmployees(empList);
        //執行儲存操作
        companyDao.save(company);
        //執行結果-->儲存 com_company 表一條資訊-->儲存emp_employee表兩條資訊,並增加外來鍵欄位com_emp_id,關聯com_company表
    }
    /**
    *刪除
    */
    @Test
    @Transactional
    @Rollback(false)
    public void test02() {
        //刪除操作
        companyDao.delete(1L);
        //執行結果-->com_company表該條資料清除,與com_company表該條資料相關聯的emp_employee表資料com_emp_id欄位值變為null
    }
}