实体   Manager

package entity;

import javax.persistence.Column;
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;
@Table(name="managers")
@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;
    }
    @Column(name="MGR_NAME")
    public String getMgrName() {
        return mgrName;
    }
    public void setMgrName(String mgrName) {
        this.mgrName = mgrName;
    }
    
    /**
     * 对于不维护关系的一方(没有外外键的一方) ,使用@OneToOne 来进行映射,建议设置mappedBy=true
     * 同时,使用了mappedBy属性,不能同时使用@JoinColumn,否组会抛出异常
     * @return
     */
    //@JoinColumn(name="MGR_ID")
    @OneToOne(mappedBy="manager",fetch=FetchType.LAZY)  //不维护,使用对方的manager来维护
    public Department getDepartment() {
        return department;
    }
    public void setDepartment(Department department) {
        this.department = department;
    }
    
    
}
View Code

 

实体   Department

package entity;

import static org.junit.Assert.assertTrue;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Table(name="department")
@Entity
public class Department {
    private Integer id;
    private  String deptName;
    private Manager manager;
    
    @GeneratedValue
    @Id
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    
    /**
     * 使用@OnetoOne 映射一对一的关联关系
     * 如要在当前表中添加主键。则需要使用@JoinColumn ,说明,一对一关系,需要添加unique=true 
     * @return
     */
    @JoinColumn(name="MGR_ID",unique=true    )   
    @OneToOne
    public Manager getManager() {
        return manager;
    }
    public void setManager(Manager manager) {
        this.manager = manager;
    }
    @Column(name="DEPT_NAME")
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    
    
    
}
View Code

 

 测试  OneToOnetest

package test;

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

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import entity.Department;
import entity.Manager;

public class OneToOneTest {

    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;
    private EntityTransaction entityTransaction;

    @Before
    public void init() {
        entityManagerFactory = Persistence.createEntityManagerFactory("JPA");
        entityManager = entityManagerFactory.createEntityManager();
        entityTransaction = entityManager.getTransaction();
        entityTransaction.begin();
    }

    @After
    public void distory() {
        entityTransaction.commit();
        entityManager.close();
        entityManagerFactory.close();
     }
    
    /**
     * 若查找不维护的一方,默认也是通过左外链接获取其关联对象
     * 可以通过  @OneToOne(fetch=FetchType.LAZY)来修改,
     *但是依然会发送sql语句来初始化其关联对象 ,不建议修改fetch属性
     */
   @Test
    public void testfind1(){
       Manager manager = entityManager.find(Manager.class, 22);
       System.out.println(manager.getMgrName());
       Department department = manager.getDepartment();
       System.out.println(department.getDeptName());
   }
   /**
    * 若查找维护的一方,默认也是通过左外链接获取其关联对象
    *可以通过  @OneToOne(fetch=FetchType.LAZY)来修改
    */
   @Test
   public void testfind2(){
       Department department = entityManager.find(Department.class, 21);
       System.out.println(department.getDeptName());
       System.out.println(department.getManager().getMgrName());
   }
    /**
     * 双向一对一关系,建议先保存不维护关系的一方(没有外键的),这样不会产生update语句
     */
    @Test
    public void testPersistence() {
        Manager manager = new Manager();
        manager.setMgrName("AAA");
        Department department = new Department();
        department.setDeptName("BBB");
        // set associate 设置关联
        manager.setDepartment(department);
        department.setManager(manager);
        // 双向一对一关系,建议先保存不维护关系的一方(没有外键的),这样不会产生update语句
        entityManager.persist(manager);
        entityManager.persist(department);

    }

}
View Code

 

posted on 2018-03-25 21:31  lshan  阅读(308)  评论(0编辑  收藏  举报