溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

          alter table wife
                  drop
                  foreign key FK37AF11D67CB035

              drop table if exists husband

              drop table if exists wife

              create table husband (
                  id integer not null auto_increment,
                  name varchar(255),
                  primary key (id)
              )

              create table wife (
                  id integer not null,
                  name varchar(255),
                  primary key (id)
              )

              alter table wife
                  add index FK37AF11D67CB035 (id),
                  add constraint FK37AF11D67CB035
                  foreign key (id)
                  references husband (id)

          實體
          Husband

          private int id;
           private String name;
           private Wife wife;

          Wife
          private int id;
           private String name;
           private Husband husband;

          Husband.hbm.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE hibernate-mapping PUBLIC 
              "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
          >
              
          <hibernate-mapping package="com.hibernate.one2one.bean">
                  
          <class name="Husband" table="husband">
                      
          <id name="id" column="id">
                          
          <generator class="native"></generator>
                      
          </id>
                      
          <property name="name"></property>
                      
          <one-to-one name="wife" cascade="all" class="Wife"></one-to-one>
                  
          </class>
              
          </hibernate-mapping>

          Wife.hbm.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE hibernate-mapping PUBLIC 
              "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
          >
              
          <hibernate-mapping package="com.hibernate.one2one.bean">
                  
          <class name="Wife" table="wife">
                      
          <id name="id" column="id">
                          
          <generator class="foreign">
                              
          <param name="property">husband</param>
                          
          </generator>
                      
          </id>
                      
          <property name="name"></property>
                      
          <one-to-one name="husband" constrained="true"></one-to-one>
                  
          </class>
              
          </hibernate-mapping>
          @Test
              
          public void insert(){
                  Session session
          =HibernateSessionFactory.getSession();
                  Transaction transaction
          =session.beginTransaction();
                  
          try {
                      transaction.begin();
                      Husband husband
          =new Husband();
                      husband.setName(
          "小明");
                      session.save(husband);
                      Wife wife
          =new Wife();
                      wife.setName(
          "如花");
                      wife.setHusband(husband);
                      session.save(wife);
                      transaction.commit();
                  } 
          catch (HibernateException e) {
                      e.printStackTrace();
                      transaction.rollback();
                  }
              }


          示例程序
          posted on 2010-10-14 10:01 雪山飛鵠 閱讀(681) 評論(0)  編輯  收藏 所屬分類: Hibernate
          主站蜘蛛池模板: 腾冲县| 天柱县| 黄大仙区| 会同县| 枝江市| 阳泉市| 呼图壁县| 徐汇区| 南康市| 岳池县| 四川省| 宜州市| 兴宁市| 精河县| 镇坪县| 遵义市| 东源县| 丽水市| 大名县| 隆昌县| 河南省| 宁城县| 大足县| 洪泽县| 绿春县| 钟山县| 襄汾县| 田东县| 准格尔旗| 手机| 保康县| 鹤庆县| 容城县| 周口市| 阳春市| 九龙坡区| 呼伦贝尔市| 边坝县| 称多县| 离岛区| 板桥市|