Hibernate之一对一双向主键关联

1、Husband.java

 

  
  
  
  
  1. package model;  
  2.  
  3. public class Husband {  
  4.     private int id;  
  5.     private String name;  
  6.     private Wife wife; //与wife关联  
  7.     public Wife getWife() {  
  8.         return wife;  
  9.     }  
  10.     public void setWife(Wife wife) {  
  11.         this.wife = wife;  
  12.     }  
  13.     public int getId() {  
  14.         return id;  
  15.     }  
  16.     public void setId(int id) {  
  17.         this.id = id;  
  18.     }  
  19.     public String getName() {  
  20.         return name;  
  21.     }  
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.       
  26. }  

2、Wife.java

 

  
  
  
  
  1. package model;  
  2.  
  3. public class Wife {  
  4.     private int id;  
  5.     private String name;  
  6.     private Husband husband;  //与Husband关联  
  7.     public int getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(int id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19.     public Husband getHusband() {  
  20.         return husband;  
  21.     }  
  22.     public void setHusband(Husband husband) {  
  23.         this.husband = husband;  
  24.     }  
  25. }  

3、Husband.java

 

  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  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 package="model"> 
  6.         <class name="Husband" table="husband" > 
  7.             <id name="id" column="id"> 
  8.                 <generator class="native"/> 
  9.             </id> 
  10.             <property name="name" column="name"/> 
  11.             <one-to-one name="wife" class="Wife" property-ref="husband"/> 
  12.         </class> 
  13.     </hibernate-mapping> 

4.Wife.hbm.xml

 

  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  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 package="model"> 
  6.         <class name="Wife" table="wife" > 
  7.             <id name="id" column="id"> 
  8.                 <generator class="native"/> 
  9.             </id> 
  10.             <property name="name" column="name" type="string"/> 
  11.             <many-to-one name="husband" column="husband_id" unique="true" /> 
  12.         </class> 
  13.     </hibernate-mapping> 

<many-to-one name="husband" column="husband_id" unique="true" />
many-to-one的作用是在wife表中生成一个外键husband_id,且唯一,并参照husband表中的主键
 

 

5、 测试

 

  
  
  
  
  1. package test;  
  2.  
  3. import model.Husband;  
  4. import model.Wife;  
  5.  
  6. import org.hibernate.HibernateException;  
  7. import org.hibernate.Session;  
  8. import org.hibernate.SessionFactory;  
  9. import org.hibernate.Transaction;  
  10. import org.hibernate.cfg.Configuration;  
  11. import org.hibernate.tool.hbm2ddl.SchemaExport;  
  12. import org.junit.Test;  
  13.  
  14. public class HibernateORMappingTest {  
  15.      @Test 
  16.     public void testCreateDB(){     
  17.         //生成表结构      
  18.         Configuration config = new Configuration().configure();     
  19.         SchemaExport export = new SchemaExport(config);     
  20.         export.create(truetrue);     
  21.     }     
  22.        
  23.      @Test 
  24.      public void testSave(){  
  25.          //测试 添加数据  
  26.           
  27.          Configuration config= new Configuration().configure();  
  28.          SessionFactory sessionfactory = config.buildSessionFactory();  
  29.            
  30.           Session session = sessionfactory.openSession();  
  31.           Transaction transaction = session.beginTransaction();  
  32.             try {  
  33.                 transaction.begin();  
  34.                 Husband husband=new Husband();  
  35.                 husband.setName("GG");  
  36.                 session.save(husband);  
  37.                 Wife wife=new Wife();  
  38.                 wife.setName("MM");  
  39.                 wife.setHusband(husband);  
  40.                 session.save(wife);  
  41.                 transaction.commit();  
  42.             } catch (HibernateException e) {  
  43.                 e.printStackTrace();  
  44.             }  
  45.  
  46.      }  
  47.        
  48.      @Test 
  49.      public void testGetPerson(){  
  50.            
  51.          Configuration config = new Configuration().configure();  
  52.          SessionFactory sessionfactory = config.buildSessionFactory();  
  53.          Session session  = sessionfactory.openSession();  
  54.          session.beginTransaction();  
  55.            
  56.          Husband husband = (Husband)session.get(Husband.class3);  
  57.          System.out.println("HusbandName is : " + husband.getName());  
  58.          System.out.println("WifeName is :" + husband.getWife().getName());  
  59.            
  60.            
  61.           
  62.  
  63.  
  64.           
  65.            
  66.          session.getTransaction().commit();  
  67.          session.close();  
  68.          sessionfactory.close();  
  69.            
  70.      }  
  71.  
  72.  
  73. }  

 

你可能感兴趣的:(Hibernate,一对一双向主键关联)