hibernate一对一主键唯一外键关联(一)

1.新建工程项目hibernate_one2one_ufk_1,添加hibernate所需Jar包和数据库驱动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.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. }

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

    IdCard.java

  1. package com.i51pro.hibernate;
  2. public class IdCard {
  3.     private int id;
  4.     
  5.     private String cardNo;
  6.     public int getId() {
  7.         return id;
  8.     }
  9.     public void setId(int id) {
  10.         this.id = id;
  11.     }
  12.     public String getCardNo() {
  13.         return cardNo;
  14.     }
  15.     public void setCardNo(String cardNo) {
  16.         this.cardNo = cardNo;
  17.     }
  18. }

    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.     </class>
  12. </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="native" />
  9.         </id>
  10.         <property name="name"/>
  11.         <many-to-one name="idCard" unique="true" />
  12.     </class>
  13. </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_ufk_1</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.在工程项目下新建SourceFolder,命名为test,在该包下编写单元测试类One2OneTest.java,进行单元测试,它的代码如下:

    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.             //不能保存,因为idCard是Transident状态
  20.             session.save(person);
  21.             
  22.             session.getTransaction().commit();
  23.         }catch(Exception e) {
  24.             e.printStackTrace();
  25.             session.getTransaction().rollback();
  26.         }finally {
  27.             HibernateUtils.closeSession(session);
  28.         }
  29.     }   
  30.         public void testSave2() {
  31.             Session session = null;
  32.             try {
  33.                 session = HibernateUtils.getSession();
  34.                 Transaction tx = session.beginTransaction();
  35.                 
  36.                 IdCard idCard = new IdCard();
  37.                 idCard.setCardNo("88888888");
  38.                 session.save(idCard);
  39.                 
  40.                 Person person = new Person();
  41.                 person.setName("李小晚");
  42.                 person.setIdCard(idCard);
  43.                 
  44.                 session.save(person);
  45.                 
  46.                 session.getTransaction().commit();
  47.             }catch(Exception e) {
  48.                 e.printStackTrace();
  49.                 session.getTransaction().rollback();
  50.             }finally {
  51.                 HibernateUtils.closeSession(session);
  52.             }
  53.     }
  54.     
  55.     public void testLoad1() {
  56.         Session session = null;
  57.         try {
  58.             session = HibernateUtils.getSession();
  59.             Transaction tx = session.beginTransaction();
  60.             
  61.             Person person = (Person)session.load(Person.class2);
  62.             System.out.println("person.name:" + person.getName() );
  63.             System.out.println("idCard.name:" + person.getIdCard().getCardNo());
  64.             
  65.             session.getTransaction().commit();
  66.         }catch(Exception e) {
  67.             e.printStackTrace();
  68.             session.getTransaction().rollback();
  69.         }
  70.     }
  71. }

 

总结:hibernate一对一唯一外键关联映射(单向关联Person---->IdCard)

一对唯一外键关联映射是多对一关联映射的特例

可以采用<many-to-one>标签,指定多的一端的unique=true,这样就限制了多的一端的多重性为一
通过这种手段映射一对一唯一外键关联


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值