hibernate :one-to-one

  1. package com.javamodel.hibernate;  
  2.  
  3. Person.java
    代码
    1. package com.javamodel.hibernate;  
    2.   
    3. public class Person {  
    4.       
    5.     private String id = null;  
    6.     private String name = null;  
    7.     private String email = null;      
    8.       
    9.     public Person(){}  
    10.   
    11.     /** 
    12.      * @return 
    13.      */  
    14.     public String getEmail() {  
    15.         return email;  
    16.     }  
    17.   
    18.     /** 
    19.      * @return 
    20.      */  
    21.     public String getId() {  
    22.         return id;  
    23.     }  
    24.   
    25.     /** 
    26.      * @return 
    27.      */  
    28.     public String getName() {  
    29.         return name;  
    30.     }  
    31.   
    32.     /** 
    33.      * @param string 
    34.      */  
    35.     public void setEmail(String string) {  
    36.         email = string;  
    37.     }  
    38.   
    39.     /** 
    40.      * @param string 
    41.      */  
    42.     public void setId(String string) {  
    43.         id = string;  
    44.     }  
    45.   
    46.     /** 
    47.      * @param string 
    48.      */  
    49.     public void setName(String string) {  
    50.         name = string;  
    51.     }  
    52.   
    53. }  
    <script type="text/javascript">render_code();</script>
  4. public class Author{  
  5.       
  6.     private String id ;  
  7.     private String alias = null;  
  8.     private Person person = null;  
  9.       
  10.     /** 
  11.      * @return 
  12.      */  
  13.     public String getAlias() {  
  14.         return alias;  
  15.     }  
  16.   
  17.       
  18.   
  19.     /** 
  20.      * @return 
  21.      */  
  22.     public Person getPerson() {  
  23.         return person;  
  24.     }  
  25.   
  26.     /** 
  27.      * @param string 
  28.      */  
  29.     public void setAlias(String string) {  
  30.         alias = string;  
  31.     }  
  32.     /** 
  33.      * @param person 
  34.      */  
  35.     public void setPerson(Person person) {  
  36.         this.person = person;  
  37.     }  
  38.     /** 
  39.      * @return 
  40.      */  
  41.     public String getId() {  
  42.         return id;  
  43.     }  
  44.     /** 
  45.      * @param i 
  46.      */  
  47.     public void setId(String i) {  
  48.         id = i;  
  49.     }  
  50.   
  51. }  
<script type="text/javascript">render_code();</script>
author.hbm.xml
代码
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >  
  3. <hibernate-mapping>  
  4.     <class name="com.javamodel.hibernate.Author" table="author" >   
  5.         <id name="id" column="id">  
  6.             <generator class="foreign">  
  7.               <param name="property">person</param>  
  8.             </generator>  
  9.         </id>  
  10.         <property name="alias" column="alias" />  
  11.         <one-to-one name="person" class="com.javamodel.hibernate.Person" cascade="all" constrained="true" />  
  12.     </class>  
  13. </hibernate-mapping>  
<script type="text/javascript">render_code();</script>
Example.java
代码
  1. package com.javamodel.hibernate;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6.   
  7. import net.sf.hibernate.HibernateException;  
  8. import net.sf.hibernate.MappingException;  
  9. import net.sf.hibernate.Session;  
  10. import net.sf.hibernate.SessionFactory;  
  11. import net.sf.hibernate.Transaction;  
  12. import net.sf.hibernate.cfg.Configuration;  
  13.   
  14. public class Example{  
  15.       
  16.     private static SessionFactory _sessions = null;  
  17.     private static Properties pops = new Properties();  
  18.       
  19.     static{  
  20.         try {  
  21.             InputStream stream = Example.class.getResourceAsStream("hibernate.properties");  
  22.             try {  
  23.                 pops.load(stream);  
  24.             } catch (IOException e1) {  
  25.                 e1.printStackTrace();  
  26.             }  
  27.               
  28.             Configuration cfg = new Configuration();  
  29.             cfg.addClass(Person.class);  
  30.             cfg.addClass(Author.class);  
  31.             cfg.setProperties(pops);  
  32.             _sessions = cfg.buildSessionFactory();  
  33.               
  34.         } catch (MappingException e) {  
  35.            e.printStackTrace();  
  36.         } catch (HibernateException e) {  
  37.            e.printStackTrace();  
  38.         }  
  39.       
  40.     }  
  41.       
  42.     public static void main(String[] args) throws HibernateException {  
  43.           
  44.         Person person = new Person();  
  45.           
  46.         person.setName("HengfeiDo");  
  47.         person.setEmail("smallduzi@sohu.com");  
  48.           
  49.         Author author = new Author();  
  50.   
  51.         author.setAlias("smallduzi");  
  52.         author.setPerson(person);  
  53.   
  54.         Session session = _sessions.openSession();  
  55.           
  56.         Transaction tx = null;  
  57.         try{  
  58.             tx = session.beginTransaction();  
  59.             session.save(author);  
  60.               
  61.             tx.commit();  
  62.             System.out.println("over");  
  63.         }catch(HibernateException he){  
  64.             if(tx != null) tx.rollback();  
  65.             throw he;  
  66.         }  
  67.         finally{  
  68.             session.close();  
  69.         }  
  70.           
  71.     }  
  72.       

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值