hibernate一对一主键关联映射(二)---双向关联

1.新建工程项目hibernate_one2one_pk_2,加入hibernate所需的Jar包和数据库mysql驱动Jar包。

2.编写工具类HibernateUtils.java和ExportDB.java,代码如下:

    HibernateUtils.java

  1. package com.i51pro.hibernate;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.cfg.Configuration;
  5. public class HibernateUtils {
  6.     private static SessionFactory sessionFactory;
  7.     static {
  8.         try {
  9.             Configuration cfg = new Configuration().configure();
  10.             sessionFactory = cfg.buildSessionFactory();
  11.         } catch (Exception e) {
  12.             e.printStackTrace();
  13.         }
  14.     }
  15.     public static SessionFactory getSessionFactory() {
  16.         return sessionFactory;
  17.     }
  18.     public static Session getSession() {
  19.         return sessionFactory.openSession();
  20.     }
  21.     public static void closeSession(Session session) {
  22.         if (session != null) {
  23.             if (session.isOpen()) {
  24.                 session.close();
  25.             }
  26.         }
  27.     }
  28. }

 

    ExportDB.java

  1. package com.i51pro.hibernate;
  2. import org.hibernate.cfg.Configuration;
  3. import org.hibernate.tool.hbm2ddl.SchemaExport;
  4. public class ExportDB {
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         Configuration cfg = new Configuration().configure();
  10.         
  11.         SchemaExport export = new SchemaExport(cfg);
  12.         export.create(truetrue);
  13.     }
  14. }

 

3.编写实体类和映射文件,代码如下:

    IdCard.java

  1. package com.i51pro.hibernate;
  2. public class IdCard {
  3.     private int id;
  4.     
  5.     private String cardNo;
  6.     private Person person;
  7.     
  8.     public int getId() {
  9.         return id;
  10.     }
  11.     public void setId(int id) {
  12.         this.id = id;
  13.     }
  14.     public String getCardNo() {
  15.         return cardNo;
  16.     }
  17.     public void setCardNo(String cardNo) {
  18.         this.cardNo = cardNo;
  19.     }
  20.     public Person getPerson() {
  21.         return person;
  22.     }
  23.     public void setPerson(Person person) {
  24.         this.person = person;
  25.     }
  26. }

    Person.java

  1. package com.i51pro.hibernate;
  2. public class Person {
  3.     private int id;
  4.     
  5.     private String name;
  6.     private IdCard idCard;
  7.     
  8.     public int getId() {
  9.         return id;
  10.     }
  11.     public void setId(int id) {
  12.         this.id = id;
  13.     }
  14.     public String getName() {
  15.         return name;
  16.     }
  17.     public void setName(String name) {
  18.         this.name = name;
  19.     }
  20.     public IdCard getIdCard() {
  21.         return idCard;
  22.     }
  23.     public void setIdCard(IdCard idCard) {
  24.         this.idCard = idCard;
  25.     }
  26. }

     IdCard.hbm.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC 
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6.     <class name="com.i51pro.hibernate.IdCard" table="t_idcard">
  7.         <id name="id">
  8.             <generator class="native"/>
  9.         </id>
  10.         <property name="cardNo"/>
  11.         <one-to-one name="person" />
  12.     </class>
  13. </hibernate-mapping>

    Person.hbm.xml

  1. <?xml version="1.0"?>
  2. <!DOCTYPE hibernate-mapping PUBLIC 
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  5. <hibernate-mapping>
  6.     <class name="com.i51pro.hibernate.Person" table="t_person">
  7.         <id name="id">
  8.             <generator class="foreign">
  9.                 <param name="property">idCard</param>
  10.             </generator>
  11.         </id>
  12.         <property name="name"/>
  13.         <one-to-one name="idCard" constrained="true" />
  14.     </class>
  15. </hibernate-mapping>

4.编写hibernate映射文件,代码如下:

    hibernate.cfg.xml

  1. <!DOCTYPE hibernate-configuration PUBLIC
  2.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  3.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  4. <hibernate-configuration>
  5.     <session-factory>
  6.         <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_one2one_pk_2</property>
  7.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  8.         <property name="hibernate.connection.username">root</property>
  9.         <property name="hibernate.connection.password">admin</property>
  10.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  11.         <property name="hibernate.show_sql">true</property>
  12.         
  13.         <mapping resource="com/i51pro/hibernate/Person.hbm.xml"/>
  14.         <mapping resource="com/i51pro/hibernate/IdCard.hbm.xml"/>
  15.     </session-factory>
  16. </hibernate-configuration>

5.在工程项目hibernate_one2one_pk_2下,新建一个SourceFolder,命名为test,在test包下编写单元测试类One2OneTest.java进行单元测试,代码如下:

  1. package com.i51pro.hibernate;
  2. import org.hibernate.Session;
  3. import org.hibernate.Transaction;
  4. import junit.framework.TestCase;
  5. public class One2OneTest extends TestCase {
  6.     public void testSave1() {
  7.         Session session = null;
  8.         try {
  9.             session = HibernateUtils.getSession();
  10.             Transaction tx = session.beginTransaction();
  11.             
  12.             IdCard idCard = new IdCard();
  13.             idCard.setCardNo("88888888");
  14.             
  15.             Person person = new Person();
  16.             person.setName("李小晚");
  17.             person.setIdCard(idCard);
  18.             
  19.             /**
  20.              * 不会发生TransientOabjectException异常
  21.              * 因为一对一主键关联映射中,默认了cascade属性
  22.              */
  23.             session.save(person);
  24.             
  25.             session.getTransaction().commit();
  26.         }catch(Exception e) {
  27.             e.printStackTrace();
  28.             session.getTransaction().rollback();
  29.         }finally {
  30.             HibernateUtils.closeSession(session);
  31.         }
  32.     }
  33.     
  34.     public void testLoad1() {
  35.         Session session = null;
  36.         try {
  37.             session = HibernateUtils.getSession();
  38.             Transaction tx = session.beginTransaction();
  39.             
  40.             Person person = (Person)session.load(Person.class1);
  41.             System.out.println("person.name:" + person.getName() );
  42.             System.out.println("idCard.name:" + person.getIdCard().getCardNo());
  43.             
  44.             session.getTransaction().commit();
  45.         }catch(Exception e) {
  46.             e.printStackTrace();
  47.             session.getTransaction().rollback();
  48.         }
  49.     }
  50.     
  51.     public void testLoad2() {
  52.         Session session = null;
  53.         try {
  54.             session = HibernateUtils.getSession();
  55.             Transaction tx = session.beginTransaction();
  56.             
  57.             IdCard idCard = (IdCard)session.load(IdCard.class1);
  58.             System.out.println("person.name:" + idCard.getPerson().getName());
  59.             System.out.println("idCard.name:" + idCard.getCardNo());
  60.             
  61.             tx.commit();
  62.         }catch(Exception e) {
  63.             e.printStackTrace();
  64.             session.getTransaction().rollback();
  65.         }finally {
  66.             HibernateUtils.closeSession(session);
  67.         }
  68.     }
  69. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值