Hibernate之one-to-one主键关联映射


one-to-one映射的例子为一个人拥有一个身份证,一个身份证属于一个人。

先创建po类

Person.java

  1. package po;  
  2.   
  3. public class Person {  
  4.     private int id;          
  5.     private String name;  //姓名   
  6.     public int getId() {  
  7.         return id;  
  8.     }  
  9.     public void setId(int id) {  
  10.         this.id = id;  
  11.     }  
  12.     public String getName() {  
  13.         return name;  
  14.     }  
  15.     public void setName(String name) {  
  16.         this.name = name;  
  17.     }  
  18.       
  19. }  
Card.java
  1. package po;  
  2.   
  3. public class Card {  
  4.     private int id;          //身份证ID   
  5.     private String number;   //身份证号码   
  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 getNumber() {  
  15.         return number;  
  16.     }  
  17.     public void setNumber(String number) {  
  18.         this.number = number;  
  19.     }  
  20.     public Person getPerson() {  
  21.         return person;  
  22.     }  
  23.     public void setPerson(Person person) {  
  24.         this.person = person;  
  25.     }  
  26.       
  27.       
  28.       
  29.       
  30. }  
po类的映射文件

Person.hbm.xml

  1. <hibernate-mapping>  
  2.     <class name="po.Person" table="person">  
  3.         <id name="id" type="integer">  
  4.             <generator class="native" />  
  5.         </id>  
  6.         <property name="name" />  
  7.     </class>  
  8.   
  9. </hibernate-mapping>  
Card.hbm.xml
  1. <hibernate-mapping>  
  2.     <class name="po.Card" table="card">  
  3.         <id name="id" type="integer">  
  4.            <!-- 主键生成方式为foreign -->  
  5.            <generator class="foreign">  
  6.                <param name="property">person</param>  
  7.            </generator>  
  8.         </id>  
  9.         <property name="number"></property>  
  10.         <!-- !!!constrained=true表示生成外键约束 -->  
  11.         <one-to-one name="person" constrained="true"></one-to-one>  
  12.     </class>  
  13. </hibernate-mapping>  
hibernate.cfg.xml
  1. <hibernate-configuration>  
  2.   
  3.     <session-factory>  
  4.        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  5.         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>  
  6.         <property name="connection.username">root</property>  
  7.         <property name="connection.password">1</property>  
  8.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  9.         <property name="myeclipse.connection.profile">mysql</property>  
  10.         <property name="show_sql">true</property>  
  11.         <property name="format_sql">true</property>  
  12.         <mapping resource="po/Person.hbm.xml"/>  
  13.         <mapping resource="po/Card.hbm.xml"/>  
  14.     </session-factory>  
  15. </hibernate-configuration  
测试类Test.java
  1. package po;  
  2.   
  3. import java.util.Date;  
  4. import java.util.HashSet;  
  5. import java.util.Iterator;  
  6. import java.util.Set;  
  7.   
  8. import org.hibernate.Session;  
  9. import org.hibernate.SessionFactory;  
  10. import org.hibernate.cfg.Configuration;  
  11. import org.hibernate.tool.hbm2ddl.SchemaExport;  
  12. import org.junit.Test;  
  13.   
  14.   
  15. public class Test {  
  16.       
  17.     @Test  
  18.     public void testCreateDB(){  
  19.         //生成表结构   
  20.         Configuration config = new Configuration().configure();  
  21.         SchemaExport export = new SchemaExport(config);  
  22.         export.create(truetrue);  
  23.     }  
  24.     @Test  
  25.     public void testSave(){  
  26.         //测试 添加数据    
  27.         Configuration config = new Configuration().configure();  
  28.         SessionFactory factory = config.buildSessionFactory();  
  29.         Session session = factory.openSession();  
  30.         session.beginTransaction();  
  31.           
  32.          
  33.         Person person = new Person();   //先有person然后才能有省份证   
  34.         person.setName("zhangsan");  
  35.           
  36.         Card card = new Card();  
  37.         card.setNumber("4114031111222223333");  
  38.         card.setPerson(person);       //此身份证属于此人   
  39.         session.save(card);           //保存card   
  40.           
  41.         session.getTransaction().commit();  
  42.           
  43.     }   
  44.     @Test  
  45.     public void testGetPerson(){  
  46.         Configuration config = new Configuration().configure();  
  47.         SessionFactory factory = config.buildSessionFactory();  
  48.         Session session = factory.openSession();  
  49.         session.beginTransaction();  
  50.   
  51.         Card card = (Card)session.get(Card.class,1 ); //取出Card   
  52.         System.out.println("CardNumber: "+card.getNumber());  
  53.           
  54.         Person person = card.getPerson();  //通过card.getPerson()方法 返回person   
  55.         System.out.println("PersonName: "+person.getName()); //取出person的name   
  56.           
  57.         session.getTransaction().commit();  
  58.         session.close();  
  59.     }  
  60. }  
hibernate后天生成表结构sql为
  1. create table card (  
  2.        id integer not null,  
  3.        number varchar(255),  
  4.        primary key (id)  
  5.    )  
  6.   
  7.    create table person (  
  8.        id integer not null auto_increment,  
  9.        name varchar(255),  
  10.        primary key (id)  
  11.    )  
  12.   alter table card   
  13.        add index FK2E7B10F83A3F5F (id),   
  14.        add constraint FK2E7B10F83A3F5F   
  15.        foreign key (id)   
  16.        references person (id)  

相关内容