这次介绍 还是以Person 和 Address 为例.. 

one to one 1对1 单向 主键关联.. 在一方关联另外一方,1对1,不能两方,同时拥有主键策略, 只能在一方设置为foreign 外键关联到另一方的主键.

 

 

下面是的具体代码:

Person_PK_One_to_One:

import java.io.Serializable;
public class Person_PK_One_to_One implements Serializable{
	private static final long serialVersionUID = -3370288005329889696L;
	
	private Integer id;
	private String name;
	private Integer age;
	private Address_PK_One_to_One addressPKOneToOne;

              //getter 和settter方法省略

}

 

Address_PK_One_to_One:

public class Address_PK_One_to_One implements Serializable{
	private static final long serialVersionUID = -1281147151620979875L;
	private Integer addressID;
	private String addressDetail;
	//getter和setter方法省略;
}

 

下面是Hibernate配置文件:

Person_PK_One_to_One.hbm.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 package="pack.java.model">
	<class name="Person_PK_One_to_One" table="PERSON_PK_ONE_TO_ONE">
		<id column="ID" name="id" type="java.lang.Integer">
			<!-- 
			    这里主键 设foreign是因为,one to one的关联时,不能两个类,同时拥有自己的主键生成策略,
			     所以,在一方作为foreign,引用到另一方的主键即可。
			 -->
			<generator class="foreign">
				<param name="property">addressPKOneToOne</param>
			</generator>
		</id>
		<property name="name" column="NAME"></property>
		<property name="age" column="AGE"></property>
		
		<!-- constrained="true" 表示,该类的主键由关联类生成。 -->
		<one-to-one name="addressPKOneToOne" constrained="true"></one-to-one>
	</class>
</hibernate-mapping>


 

Address_PK_One_to_One.hbm.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 package="pack.java.model">
	<class name="Address_PK_One_to_One" table="ZHT_ADDRESS_PK_ONE_TO_ONE">
		<id name="addressID" column="ADDRESS_ID" type="java.lang.Integer">
			<generator class="identity"></generator>
		</id>
		
		<!-- 地址详情 -->
		<property name="addressDetail" column="ADDRESSDETAIL" type="java.lang.String"></property>
	</class>
</hibernate-mapping>


 

写好配置文件后,接下来在Hibernate.cfg.xml中加入 刚刚新建的两个Mapping映射文档.

<mapping resource="pack/java/model/Person_PK_One_to_One.hbm.xml"/>

<mapping resource="pack/java/model/Address_PK_One_to_One.hbm.xml"/>

 

 

之后,就新建一个Test类,

用来测试1对1 主键关联的映射关系.

package pack.java.test;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pack.java.hibernate.HibernateSessionFactory;
import pack.java.model.Address_PK_One_to_One;
import pack.java.model.Person_PK_One_to_One;

public class HibernateDemoOneTo_One {
	public static void main(String[]args){
		HibernateDemoOneTo_One demoOneToOne = new HibernateDemoOneTo_One();
		Session session = demoOneToOne.getSession();
		
		//保存;
		demoOneToOne.savePersonAndAddress(session);
		
		//查询;
		demoOneToOne.queryPerson(session);
	}
	
	/**
	 * 保存Person和Address;
	 * @param session
	 */
	private void savePersonAndAddress(Session session){
		Address_PK_One_to_One addressPKOneToOne = new Address_PK_One_to_One();
		addressPKOneToOne.setAddressDetail("湖南省株洲市");
		
		Person_PK_One_to_One personPKOneToOne = new Person_PK_One_to_One();
		personPKOneToOne.setName("ZhouHaiTao");
		personPKOneToOne.setAge(23);
		personPKOneToOne.setAddressPKOneToOne(addressPKOneToOne);
		
		//开启事务;
		session.beginTransaction().begin();
		//保存操作;
		session.save(personPKOneToOne);
		System.out.println("保存成功!");
		session.beginTransaction().commit();
		
		//关闭session;
		session.close();
	}
	
	/**
	 * 查询Person对象和Address对象;
	 * @param session
	 */
	private void queryPerson(Session session){
		
		if(session!=null && !session.isOpen()){
			session = HibernateSessionFactory.getSession();
		}
		String hql="select p from Person_PK_One_to_One p where p.id = :pid";
		Query query = session.createQuery(hql);
		query.setParameter("pid",1);
		
		List<Person_PK_One_to_One> list = query.list();
		for (Person_PK_One_to_One personPKOneToOne : list) {
			System.out.println(personPKOneToOne.getId()+","+personPKOneToOne.getName()+","+personPKOneToOne.getAge()+","+personPKOneToOne.getAddressPKOneToOne().getAddressDetail());
		}
		
		session.close();
	}
	
	/**
	 * 获取session;
	 * @return
	 */
	private Session getSession(){
		Session session = HibernateSessionFactory.getSession();
		return session;
	}
}