JPA笔记3 OneToOne

 

package one_to_one;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.sun.org.apache.xml.internal.resolver.readers.TR9401CatalogReader;

@Table(name = "JPA_DEPARTMENT")
@Entity
public class Department {

    private Integer id;
    private String name;

    private Manager manager;

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //注意:1-2关联关系,需要添加unique = true
    @JoinColumn(name = "mgr_id", unique = true)
    @OneToOne(fetch=FetchType.LAZY)
    public Manager getManager() {
        return manager;
    }

    public void setManager(Manager manager) {
        this.manager = manager;
    }

}

 

package one_to_one;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Table(name = "JPA_MANAGER")
@Entity
public class Manager {
    private Integer id;
    private String mgrName;
    private Department department;

    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getMgrName() {
        return mgrName;
    }

    public void setMgrName(String mgrName) {
        this.mgrName = mgrName;
    }

    @OneToOne(mappedBy = "manager")
    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

}

 

package one_to_one;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class Main3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String persistenceUnitName = "jpa-1";
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);

        EntityManager entityManager = entityManagerFactory.createEntityManager();

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        // oneToOnePersist(entityManager);
        oneToOneFind(entityManager);

        transaction.commit();
        entityManager.close();
        entityManagerFactory.close();
    }

    // 建议先保存不维护关联关系的一方,即没有外键的一方,这样不会多出update语句
    private static void oneToOnePersist(EntityManager entityManager) {
        Manager manager = new Manager();
        manager.setMgrName("M-AA");

        Department department = new Department();
        department.setName("D-AA");

        manager.setDepartment(department);
        department.setManager(manager);

        entityManager.persist(manager);
        entityManager.persist(department);
    }

    // 默认通过左连接加载对象
    // 如果通过设置@OneToOne的fetch属性为lazy,还是会用左连接,且会多查询
    private static void oneToOneFind(EntityManager entityManager) {
        Department dept = entityManager.find(Department.class, 1);
        System.out.println(dept);
        System.out.println(dept.getManager().getMgrName());
    }
}

 

posted on 2019-09-20 19:49  上校  阅读(271)  评论(0编辑  收藏  举报