hibernate一对一关系(foregin)

原创
2016/12/02 17:12
阅读数 36

1.经理.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
	<class name="com.huangliusong.one2one.foreign.Maneger" table="manegers">
		<id name="m_id" type="java.lang.Integer">
			<column name="m_id" ></column>
			<generator class="native"></generator>
		</id>
		<property name="m_name" type="java.lang.String">
			<column name="m_name"></column>
		</property>
		<!-- 	映射1对1的关联关系 在对应的数据表中已经有外键了 当前持久化使用one2one进行映射 -->
		<one-to-one name="department" 
		class="com.huangliusong.one2one.foreign.Department" property-ref="mgr">
			
		</one-to-one>
	</class>
</hibernate-mapping>

2.部门.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
	<class name="com.huangliusong.one2one.foreign.Department" table="departments">
		<id name="d_id" type="java.lang.Integer">
			<column name="d_id"></column>
			<generator class="native"></generator>
		</id>
		<property name="d_name" type="java.lang.String">
			<column name="d_name"></column>
		</property>
		
		<!-- 使用many1one的方式来映射 一对一的关联关系 -->
		<many-to-one name="mgr" class="com.huangliusong.one2one.foreign.Maneger" column="m_id" update="true">
			
		</many-to-one>
	</class>
</hibernate-mapping>

3.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
		<property name="connection.username">root</property>
		<property name="connection.password">linuxG</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/hls</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property><!-- 格式化sql数据库 -->
		<property name="hbm2ddl.auto">update</property>
		<!-- 配置C3P0数据源  -->
		<property name="hibernate.c3p0.max_size">10</property>
		<property name="hibernate.c3p0.min_size">5</property>
		<property name="c3p0.acquire_increment">2</property>
		
		<property name="c3p0.idle_test_period">2000</property>
		<property name="c3p0.timeout">2000</property>
		
		<property name="c3p0.max_statements">10</property>
		
		<!-- 设定jdbc 的statement读取数据的时候每次从数据库中取出记录的条数 -->
		<property name="hibernate.jdbc.fetch_size">100</property>
		
		<!-- 设定数据库进行批量删除的时候 批量更新 的时候 批次的大小 -->
		<property name="hibernate.jdbc.batch_size">30</property>
		<!-- 删除对象后将OID置为空 -->
		<property name="use_identifier_rollback">true</property>
		
	<mapping resource="com/huangliusong/one2one/foreign/Maneger.hbm.xml"/>
	<mapping resource="com/huangliusong/one2one/foreign/Department.hbm.xml"/>
    </session-factory>
	
</hibernate-configuration>

4.测试

package com.huangliusng.test;

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;

import com.huangliusong.one2one.foreign.Department;
import com.huangliusong.one2one.foreign.Maneger;
import com.mchange.v1.cachedstore.CachedStore.Manager;
import com.sun.mail.handlers.message_rfc822;

public class TestHibernate4 {
	private static SessionFactory sessionFactory;
	private static Session session;
	private static Transaction transaction;

	@Before
	public void init() {
		// 1.创建配置对象
		Configuration config = new Configuration().configure();
		// 2.服务注册对象
		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
				.applySettings(config.getProperties()).buildServiceRegistry();
		// 3.创建会话工厂
		sessionFactory = config.buildSessionFactory(serviceRegistry);
		// 4.创建会话
		session = sessionFactory.openSession();
		// 5.打开会话
		transaction = session.beginTransaction();
	}

	@After
	public void destory() {
		transaction.commit();
		session.close();
		sessionFactory.close();
	}

	@Test
	public void OneToOne() {
		Department department=new Department();
		department.setD_name("技术部B2");
		Maneger maneger=new Maneger();
		maneger.setM_name("技术部经理B2");
		
		department.setMgr(maneger);
		maneger.setDepartment(department);
		
		session.save(department);
		session.save(maneger);
	}
	@Test 
	public void testGet(){
		//默认对关联属性使用懒加载
		Maneger maneger=(Maneger)session.get(Maneger.class, 1);
		System.out.println(maneger.getM_id());
		System.out.println(maneger.getDepartment().getD_name());
		
		/*System.out.println("==========");
		Department department=(Department)session.get(Department.class, 1);
		System.out.println(department.getMgr().getM_name());*/
		
	}

}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部