阅读 13

【JPA专题】09.映射关系_一对一双向

映射关系(一对一双向)

我们使用的部门和部门主管的一对一关系

基础关系代码:部门主管类

package com.os.model;

import javax.persistence.*;

@Entity
@Table(name = "jpa_manager")
public class Manager {
    private Integer managerId;
    private String name;

    private Department dept;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "manager_id")
    public Integer getManagerId() {
        return managerId;
    }

    public void setManagerId(Integer managerId) {
        this.managerId = managerId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @OneToOne
    @JoinColumn(name = "dept_id",unique = true)
    public Department getDept() {
        return dept;
    }

    public void setDept(Department dept) {
        this.dept = dept;
    }
}
复制代码

@JoinColumn(name = "dept_id",unique = true)

设置外键dept_id,在设置该外键唯一性,这样就完成了一对一的操作

基础关系代码:部门类

package com.os.model;

import javax.persistence.*;

@Entity
@Table(name = "jpa_dept")
public class Department {
    private Integer departmentId;
    private String name;

    private Manager manager;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "dept_id")
    public Integer getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Integer departmentId) {
        this.departmentId = departmentId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @OneToOne(mappedBy = "dept")
    public Manager getManager() {
        return manager;
    }

    public void setManager(Manager manager) {
        this.manager = manager;
    }
}
复制代码

@OneToOne(mappedBy = "dept") 设置对方维护关系

公共部分代码

public class One2OneTest {

    private EntityManagerFactory factory ;
    private EntityManager entityManager ;
    private EntityTransaction tx;
    @Before
    public void init(){
        factory = Persistence.createEntityManagerFactory("jpa03");
        entityManager = factory.createEntityManager();
        tx = entityManager.getTransaction();
        tx.begin();
    }
    @After
    public void close(){
        tx.commit();
        entityManager.close();
        factory.close();
    }
}
复制代码

1.先保存部门(没有维护关系的权力),再保存主管(有维护关系的权力)

@Test
public void test01(){
    Manager manager = new Manager();
    manager.setName("小白");

    Department department = new Department();
    department.setName("工程部");

    //设置维护关系
    manager.setDept(department);
    department.setManager(manager);

    //先保存部门,再保存主管
    entityManager.persist(department);
    entityManager.persist(manager);
}
复制代码

生成的SQL语句,因为先产生的部门,外键已经产生了。

Hibernate: insert into jpa_dept (name) values (?)
Hibernate: insert into jpa_manager (dept_id, name) values (?, ?)
复制代码

2.先保存主管(有维护关系的权力),再保存部门(没有维护关系的权力)

@Test
public void test02(){
    Manager manager = new Manager();
    manager.setName("小白");

    Department department = new Department();
    department.setName("工程部");

    //设置维护关系
    manager.setDept(department);
    department.setManager(manager);

    //先保存主管,再保存部门
    entityManager.persist(manager);
    entityManager.persist(department);

}
复制代码

生成的SQL语句

Hibernate: insert into jpa_manager (dept_id, name) values (?, ?)
Hibernate: insert into jpa_dept (name) values (?)
Hibernate: update jpa_manager set dept_id=?, name=? where manager_id=?
复制代码

3.获取某个主管信息,查看一下部门对象的情况

@Test
public void test03(){
    Manager manager = entityManager.find(Manager.class,1);
    System.out.println(manager.getName());
}
复制代码

默认情况下是使用左外连接获取其对象信息,为了测试我设置为延迟加载进行测试

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "dept_id",unique = true)
public Department getDept() {
    return dept;
}
复制代码
@Test
public void test03(){
    Manager manager = entityManager.find(Manager.class,1);
    System.out.println(manager.getName());
	//特别注意这个对象的输出情况
    System.out.println(manager.getDept().getClass().getName());
}
复制代码

控制台的输出情况

Hibernate: select manager0_.manager_id as manager_1_2_0_, manager0_.dept_id as dept_id3_2_0_, manager0_.name as name2_2_0_ from jpa_manager manager0_ where manager0_.manager_id=?
小白
-- 产生了代理对象
com.os.model.Department$HibernateProxy$2dCi3F6H
复制代码

3.获取某个部门信息,查看一下主管对象的情况

//设置成延迟加载进行测试
@OneToOne(mappedBy = "dept",fetch = FetchType.LAZY)
public Manager getManager() {
    return manager;
}
复制代码
@Test
public void test04(){
    Department dept = entityManager.find(Department.class,2);
    System.out.println(dept.getName());

    System.out.println(dept.getManager().getClass().getName());
}
复制代码

不管是默认方式还是延迟方式,主管对象不是代理对象,SQL语句都会查询

Hibernate: select department0_.dept_id as dept_id1_1_0_, department0_.name as name2_1_0_ from jpa_dept department0_ where department0_.dept_id=?
Hibernate: select manager0_.manager_id as manager_1_2_0_, manager0_.dept_id as dept_id3_2_0_, manager0_.name as name2_2_0_ from jpa_manager manager0_ where manager0_.dept_id=?
工程部
com.os.model.Manager
复制代码
关注下面的标签,发现更多相似文章
评论