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

          雪山飛鵠

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

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

          實體
          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  屬性會在wife這端添加一個外鍵約束
          添加上上述屬性使用hbm2ddl導出表,打印出的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();
                  }
              }
          溫馨提醒:此處必須同時設置
          wife.setHusband(husband);
          wife.setId(husband.getId());
          否則報org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

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

          Feedback

          # re: Hibernate一對一主鍵雙向關聯映射(Annotation配置) 2013-04-20 15:25 cjl
          wife.setId(husband.getId());這個其實沒有必要,你還有設置沒設好  回復  更多評論
            

          主站蜘蛛池模板: 青田县| 泊头市| 兴城市| 万年县| 武功县| 昭通市| 海原县| 昭觉县| 台湾省| 易门县| 蒙山县| 郧西县| 南通市| 乌拉特后旗| 贺州市| 南康市| 阿图什市| 壶关县| 凤冈县| 嘉祥县| 类乌齐县| 尉氏县| 长顺县| 鲜城| 什邡市| 买车| 新乡县| 南宁市| 嘉义市| 通化市| 阳新县| 忻城县| 合作市| 遵化市| 常州市| 固安县| 延长县| 乐昌市| 莲花县| 屏南县| 邢台市|