溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

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

          實(shí)體
          Husband
          package com.hibernate.one2one.bean;

          import javax.persistence.Column;
          import javax.persistence.Entity;
          import javax.persistence.GeneratedValue;
          import javax.persistence.GenerationType;
          import javax.persistence.Id;
          import javax.persistence.OneToOne;
          import javax.persistence.PrimaryKeyJoinColumn;
          import javax.persistence.Table;

          @Entity
          @Table(name
          ="husband")
          public class Husband {
              
              
          private int id;
              
          private String name;
              
          private Wife wife;
              @Id
              @GeneratedValue(strategy
          =GenerationType.AUTO)
              @Column(name
          ="id")
              
          public int getId() {
                  
          return id;
              }
              
          public void setId(int id) {
                  
          this.id = id;
              }
              @Column(name
          ="name")
              
          public String getName() {
                  
          return name;
              }
              
          public void setName(String name) {
                  
          this.name = name;
              }
              @OneToOne
              @PrimaryKeyJoinColumn
              
          public Wife getWife() {
                  
          return wife;
              }
              
          public void setWife(Wife wife) {
                  
          this.wife = wife;
              }
              
          }
          Wife
          package com.hibernate.one2one.bean;

          import javax.persistence.Column;
          import javax.persistence.Entity;
          import javax.persistence.Id;
          import javax.persistence.OneToOne;
          import javax.persistence.PrimaryKeyJoinColumn;
          import javax.persistence.Table;

          @Entity
          @Table(name
          ="wife")
          public class Wife {
              
              
          private int id;
              
          private String name;
              
          private Husband husband;
              @Id
              @Column(name
          ="id")
              
          public int getId() {
                  
          return id;
              }
              
          public void setId(int id) {
                  
          this.id = id;
              }
              @Column(name
          ="name")
              
          public String getName() {
                  
          return name;
              }
              
          public void setName(String name) {
                  
          this.name = name;
              }
              @OneToOne(optional
          =false)
              @PrimaryKeyJoinColumn
              
          public Husband getHusband() {
                  
          return husband;
              }
              
          public void setHusband(Husband husband) {
                  
          this.husband = husband;
              }
              
          }
          溫馨提示:注意wife.java里面的@OneToOne(optional=false)   optional=false  屬性會(huì)在wife這端添加一個(gè)外鍵約束
          添加上上述屬性使用hbm2ddl導(dǎo)出表,打印出的sql語句
          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)

          @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);
                      wife.setId(husband.getId());
                      session.save(wife);
                      transaction.commit();
                  } 
          catch (HibernateException e) {
                      e.printStackTrace();
                      transaction.rollback();
                  }
              }
          @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);
                      wife.setId(husband.getId());
                      session.save(wife);
                      transaction.commit();
                  } 
          catch (HibernateException e) {
                      e.printStackTrace();
                      transaction.rollback();
                  }
              }
          溫馨提醒:此處必須同時(shí)設(shè)置
          wife.setHusband(husband);
          wife.setId(husband.getId());
          否則報(bào)org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

          示例程序
          posted on 2010-10-14 10:28 雪山飛鵠 閱讀(2237) 評(píng)論(1)  編輯  收藏 所屬分類: Hibernate

          Feedback

          # re: Hibernate一對(duì)一主鍵雙向關(guān)聯(lián)映射(Annotation配置) 2013-04-20 15:25 cjl
          wife.setId(husband.getId());這個(gè)其實(shí)沒有必要,你還有設(shè)置沒設(shè)好  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 拉萨市| 通辽市| 鱼台县| 青岛市| 阿巴嘎旗| 龙井市| 南川市| 越西县| 荥经县| 石屏县| 洛宁县| 大悟县| 行唐县| 镇康县| 林甸县| 临漳县| 资阳市| 高邑县| 吉隆县| 松阳县| 江门市| 二连浩特市| 石嘴山市| 孟津县| 株洲县| 镇坪县| 彰化县| 盱眙县| 沂源县| 平果县| 娄底市| 元谋县| 陆丰市| 定陶县| 晋江市| 南昌市| 上虞市| 高碑店市| 镇雄县| 秭归县| 杭锦旗|