Hibernate关联映射-one to one 外键双向关联.

 

说明:双向一对一,是一对多的特例,只需要在多的一方设置成 unique=“true” 即可.

 

 

Person_SX_FK_One_to_One Pojo代码:

/**
 * 双向一对一关联;
 * @author Administrator
 *
 */
public class Person_SX_FK_One_to_One {
	private Integer id;
	private String name;
	private Integer age;
	private Address_SX_One_to_One addressSXOneToOne;

      //getter和setter方式省略.
}

 

Address_SX_One_to_One:

public class Address_SX_One_to_One implements Serializable{
	private static final long serialVersionUID = -8252905495596205673L;
	private Integer addressID;
	private String addressDetail;
	private Person_SX_FK_One_to_One personSXFKOneToOne;
    
     //getter和setter方式省略.	

}

 

 

下面是Hibernate配置文件:

Person_SX_FK_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>
	<class name="pack.java.model.Person_SX_FK_One_to_One" table="ZHT_PERSON_SX_FK_ONE_TO_ONE">
		<id name="id" column="ID">
			<generator class="identity"></generator>
		</id>
		
		<property name="name" column="NAME"></property>
		<property name="age" column="AGE"></property>
		<!-- 一对一配置,name指定person 类中的,address对象;class指定address的具体包和类。 -->
		<one-to-one name="addressSXOneToOne" class="pack.java.model.Address_SX_One_to_One" cascade="all"></one-to-one>
	</class>
</hibernate-mapping>

 

Address_SX_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>
	<class name="pack.java.model.Address_SX_One_to_One" table="ZHT_ADDRESS_SX_FK_ONE_TO_ONE">
		<id column="ADDRESSID" name="addressID">
			<generator class="identity"></generator>
		</id>
		<property name="addressDetail" column="ADDRESS_DETAIL"></property>
		
		<!-- 一对一的关联,是一对多的特例,只需要在多的一方,家在unique="true" 即可。 -->
		<many-to-one name="personSXFKOneToOne" unique="true" fetch="select" class="pack.java.model.Person_SX_FK_One_to_One" cascade="all"></many-to-one>
	</class>
</hibernate-mapping>

 

在Hibernate.cfg.xml中配置刚刚加入的两个映射文件..

 

然后,在写测试类,进行测试双向一对一关联关系。

 

package pack.java.test;

import org.hibernate.Session;
import pack.java.hibernate.HibernateSessionFactory;
import pack.java.model.Address_SX_One_to_One;
import pack.java.model.Person_SX_FK_One_to_One;
/**
 * 双向一对一外键关联,
 * @author ZhouHaiTao;
 *
 */
public class HibernateDemoSX_OneToOne {
	/**
	 * 测试方法;
	 * @param args
	 */
	public static void main(String[] args) {
		HibernateDemoSX_OneToOne demoSXOneToOne = new HibernateDemoSX_OneToOne();
		
		//保存;
		demoSXOneToOne.savePerson();
		
		//根据Person编号查询:Person对象;
		demoSXOneToOne.queryPersonByPersonID(1);
		
		//根据Address编号,查询出Address对象;
		demoSXOneToOne.queryAddressByAddressID(1);
	}
	
	/**
	 * 保存Person;
	 */
	private void savePerson(){
		Session session = HibernateSessionFactory.getSession();
		Person_SX_FK_One_to_One person = new Person_SX_FK_One_to_One();
		
		Address_SX_One_to_One address = new Address_SX_One_to_One();
		address.setAddressDetail("广东省广州市");
		
		person.setAge(23);
		person.setName("王五");
		person.setAddressSXOneToOne(address);
		address.setPersonSXFKOneToOne(person);
		
		session.beginTransaction().begin();
		//保存person;
		session.save(person);
		session.beginTransaction().commit();
		//关闭session;
		session.close();
	}
	
	/**
	 * 根据Person编号,查询 Person;
	 * @param personId
	 */
	private void queryPersonByPersonID(Integer personId){
		Session session = HibernateSessionFactory.getSession();
		//查询出person;
		Person_SX_FK_One_to_One person = (Person_SX_FK_One_to_One) session.load(Person_SX_FK_One_to_One.class, personId);
		System.out.println("根据Person编号,找出Person对象,然后,通过Person关联到Address,输出Address对象信息:");
		//输出person对象中的信息;
		System.out.println(person.getId()+","+person.getName()+","+person.getAge()+","+person.getAddressSXOneToOne().getAddressDetail());
		
	}
	
	/**
	 * 根据Address编号,查询出Address;a
	 * @param addressID
	 */
	private void queryAddressByAddressID(Integer addressID){
		Session session = HibernateSessionFactory.getSession();
		Address_SX_One_to_One addressSXOneToOne = (Address_SX_One_to_One) session.load(Address_SX_One_to_One.class, addressID);
		System.out.println("根据Address编号,找出Address对象,然后,通过Address关联到Person,输出Person对象信息:");
		System.out.println(addressSXOneToOne.getAddressID()+","+addressSXOneToOne.getAddressDetail()+","+addressSXOneToOne.getPersonSXFKOneToOne().getId()+","+addressSXOneToOne.getPersonSXFKOneToOne().getName());
	}
}

 

 

下面是控制台输出结果:

根据Person编号,找出Person对象,然后,通过Person关联到Address,输出Address对象信息:
1,王五,23,广东省广州市


根据Address编号,找出Address对象,然后,通过Address关联到Person,输出Person对象信息:
1,广东省广州市,1,王五

 

 

你可能感兴趣的:(Hibernate)