溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處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
          主站蜘蛛池模板: 嘉定区| 瑞安市| 涟源市| 台湾省| 迁安市| 新乡市| 读书| 池州市| 保山市| 固镇县| 沂水县| 内丘县| 安平县| 平罗县| 灌南县| 奉化市| 西乌| 遵义市| 翁源县| 阜阳市| 淄博市| 漠河县| 神木县| 垦利县| 临泉县| 错那县| 奉贤区| 城固县| 卢湾区| 泸州市| 马边| 荥阳市| 普陀区| 隆回县| 绍兴市| 浦县| 正镶白旗| 泰和县| 荣昌县| 佛冈县| 英德市|