基于外键映射的 一对一关系

一.

基于外键映射的 一对一关系_第1张图片

二.代码实现

1.Department

package cn.edu.sdut.hibernate.onetoone;


public class Department {

	private  int deId;
	private String deName;
	private Manager mgr;
	public int getDeId() {
		return deId;
	}
	public void setDeId(int deId) {
		this.deId = deId;
	}
	public String getDeName() {
		return deName;
	}
	public void setDeName(String deName) {
		this.deName = deName;
	}
	public Manager getMgr() {
		return mgr;
	}
	public void setMgr(Manager mgr) {
		this.mgr = mgr;
	}
	
	
	
}

2. Manager
package cn.edu.sdut.hibernate.onetoone;


public class Manager {

	private int maId;
	private String mName;
	private Department department;
	public int getMaId() {
		return maId;
	}
	public void setMaId(int maId) {
		this.maId = maId;
	}
	public String getmName() {
		return mName;
	}
	public void setmName(String mName) {
		this.mName = mName;
	}
	public Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	
	
}
3.Department.hbm.xml





    
        
            
            
        
        
            
        
        
        	
        
    


4.Manager.hbm.xml




    
        
            
            
        
        
            
        
        
    

5.DoubleOneToOneTest

package cn.edu.sdut.hibernate.onetoone;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


public class DoubleOneToOneTest {

	private SessionFactory sessionFactory;
	private Session session;
	private Transaction transaction;
	
	@Before
	public void init(){
		Configuration configuration = new Configuration().configure();
		ServiceRegistry serviceRegistry = 
				new ServiceRegistryBuilder().applySettings(configuration.getProperties())
											.buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		session = sessionFactory.openSession();
		transaction = session.beginTransaction();
	}
	@After
	public void destory(){
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
	
	@Test
	public void testSave(){
		Manager manager = new Manager();
		manager.setmName("csc");
		Department department = new Department();
		department.setDeName("AA");
		department.setMgr(manager);
		manager.setDepartment(department);
		//保存的时候先保存1to1的,后保存nto1(设置为unique = true),效率高
		session.save(manager);
		session.save(department);
	
	}
	
	@Test
	public void testGet(){
		//get获取的时候不会出现懒加载异常,因为在得到manger的时候会把所有的信息加载出来
		Manager manager = (Manager) session.get(Manager.class, 1);
		System.out.println(manager.getDepartment().getClass().getName());
	}
	
	@Test
	public void testDelete(){
		//再不设置联级关系的时候不能联级删除
		Manager manager = (Manager) session.get(Manager.class, 2);
//		Department department = (Department) session.get(Department.class, 1);
//		session.delete(department);
		session.delete(manager);
	}
}
6、hibernate.cfg.xml




    
    	
    	
    	root
    	csc
    	com.mysql.jdbc.Driver
    	jdbc:mysql:///hibernate5
    
    	
    	
    	org.hibernate.dialect.MySQLInnoDBDialect
    	
    	
    	true
    	
    	
    	true
    	
    	
    	update
    	
    	
    	true
    	
    	
    	2
    	
    	
    	100
    	20
    	5
    	2000
    	2000
    	10
    	
    	
    	100
    	
    	30
    	
    	
    	
    	
    	
    	
    	
    	
    





你可能感兴趣的:(Hibernate)