这次,继续上一次的实例。 记录1对1 主键双向关联.
还是Person和 Address为例.
请直接看代码:
Person_SX_PK_One_to_One:
public class Person_SX_PK_One_to_One { private Integer id; private String name; private Integer age; private Address_SX_PK_One_To_One addressSXPKOneToOne; //getter和setter方法省略 }
Address_SX_PK_One_To_One:
public class Address_SX_PK_One_To_One { private Integer id; private String addressDetail; private Person_SX_PK_One_to_One personSXPKOneToOne; //getter和setter方式省略 }
Hibernate配置文件:
Person_SX_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> <class name="pack.java.model.Person_SX_PK_One_to_One" table="ZHT_PERSON_SX_PK_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> <!-- 1对1,引用address对象, --> <one-to-one name="addressSXPKOneToOne" class="pack.java.model.Address_SX_PK_One_To_One" fetch="select" lazy="false" cascade="all"> </one-to-one> </class> </hibernate-mapping>
Address_SX_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> <class name="pack.java.model.Address_SX_PK_One_To_One" table="ZHT_ADDRESS_SX_PK_ONE_TO_ONE"> <id column="ID" name="id"> <!-- generator 主键生成策略,配制成foreign,外键,是引用主表person的对象。 --> <generator class="foreign"> <param name="property">personSXPKOneToOne</param> </generator> </id> <property name="addressDetail" column="ADDRESSDETAIL"></property> <!-- 表示在address表中,存在外键约束,外键参考相关表person对象。 --> <one-to-one name="personSXPKOneToOne" constrained="true"></one-to-one> </class> </hibernate-mapping>
在Hibernate.cfg.xml配置刚刚添加的两个映射hibernate.hbm.xml文件。
配置好之后,就进行测试.
写一个Test类,
package pack.java.test; import org.hibernate.Session; import pack.java.hibernate.HibernateSessionFactory; import pack.java.model.Address_SX_PK_One_To_One; import pack.java.model.Person_SX_PK_One_to_One; /** * 1对1,主键双向关联。 * @author Administrator * */ public class HibernateDemoXS_PK_OneToOne { public static void main(String[] args) { HibernateDemoXS_PK_OneToOne demoXSPKOneToOne = new HibernateDemoXS_PK_OneToOne(); Session session = demoXSPKOneToOne.getSession(); //保存操作; demoXSPKOneToOne.savePerson(session); //查询; Person_SX_PK_One_to_One personSXPKOneToOne = demoXSPKOneToOne.queryPersonBy(1); System.out.println("\n结果查询如下:"); System.out.println(personSXPKOneToOne.getId()+","+personSXPKOneToOne.getName()+","+personSXPKOneToOne.getAge()+","+personSXPKOneToOne.getAddressSXPKOneToOne().getAddressDetail()); //更新; demoXSPKOneToOne.updatePersonByPersonID(1); //再次查询; personSXPKOneToOne = demoXSPKOneToOne.queryPersonBy(1); System.out.println("\n结果查询如下:"); System.out.println(personSXPKOneToOne.getId()+","+personSXPKOneToOne.getName()+","+personSXPKOneToOne.getAge()+","+personSXPKOneToOne.getAddressSXPKOneToOne().getAddressDetail()); } /** * 根据Person编号,查询Person对象; * @param personID */ private Person_SX_PK_One_to_One queryPersonBy(Integer personID){ //获取session; Session session = getSession(); Person_SX_PK_One_to_One personSXPKOneToOne = (Person_SX_PK_One_to_One) session.load(Person_SX_PK_One_to_One.class, personID); return personSXPKOneToOne; } /** * 更新操作; * @param personID */ private void updatePersonByPersonID(Integer personID){ //先查询出person,然后,在更新; Person_SX_PK_One_to_One personSXPKOneToOne = queryPersonBy(personID); personSXPKOneToOne.setName("张无忌"); personSXPKOneToOne.setAge(23); //获取session; Session session = getSession(); //开始事务,更新操作, 提交事务。 session.beginTransaction().begin(); session.update(personSXPKOneToOne); session.beginTransaction().commit(); session.close(); } /** * 保存 * @param session */ private void savePerson(Session session){ Person_SX_PK_One_to_One person = new Person_SX_PK_One_to_One(); person.setAge(63); person.setName("张三丰"); Address_SX_PK_One_To_One address = new Address_SX_PK_One_To_One(); address.setAddressDetail("北京市朝阳区东三环北路"); //给person设置address; person.setAddressSXPKOneToOne(address); //给address设置person; address.setPersonSXPKOneToOne(person); //保存操作; session.beginTransaction().begin(); session.save(person); session.beginTransaction().commit(); session.close(); System.out.println("保存成功!"); } /** * 获取session; * @return */ private Session getSession(){ return HibernateSessionFactory.getSession(); } }
控制台输出如下:
保存成功!
结果查询如下:
1,张三丰,63,北京市朝阳区东三环北路
结果查询如下:
1,张无忌,23,北京市朝阳区东三环北路