甜咖啡

          我的IT空間

          hibernate關系映射----多對一單向關聯

          一個客戶可以對應多個訂單。

          表結構:兩張表使用powerdesiner設計生成,產生引用錯誤。

          出現問題:1.定義的級聯關系只安一中方式生成,即雙向的所以刪除了parent表的set屬性,同時刪除映射文件的set集合,

                         2.沒有定義級聯關系cascade屬性

           

          drop table if exists Customer1;

          drop table if exists OrderC1;

          /*==============================================================*/
          /* Table: Customer1                                             */
          /*==============================================================*/
          create table Customer1
          (
             id                   int not null auto_increment,
             name                 varchar(20),
             primary key (id)
          )
          type = InnoDB;

          /*==============================================================*/
          /* Table: OrderC1                                               */
          /*==============================================================*/
          create table OrderC1
          (
             id                   int not null,
             order_id             varchar(20),
             customer_id          int not null,
             primary key (id)
          )
          type = InnoDB;

          alter table OrderC1 add constraint FK_Reference_1111 foreign key (customer_id)
                references Customer1 (id) on delete restrict on update restrict;

          注意:因為有外鍵約束,需要事務支持,在安裝數據庫的時候,需要配置mysql數據庫服務器的參數。數據庫的引擎應該用InnoDB

          二、通過myeclipse生成實體和配置文件:
                     

          package many_one;

          @SuppressWarnings("serial")
          public class Customer1  implements java.io.Serializable {
               private Integer id;
               private String name;
              public Customer1() {
              }
              public Customer1(String name ) {
                  this.name = name;
              }

              public Integer getId() {
                  return this.id;
              }
             
              public void setId(Integer id) {
                  this.id = id;
              }

              public String getName() {
                  return this.name;
              }
             
              public void setName(String name) {
                  this.name = name;
              }
          }

           

          @SuppressWarnings("serial")
          public class Orderc1  implements java.io.Serializable {

               private Integer id;
               private Customer1 customer1;
               private String orderId;

              public Orderc1() {
              }

              public Orderc1(Customer1 customer1) {
                  this.customer1 = customer1;
              }
             
              public Orderc1(Customer1 customer1, String orderId) {
                  this.customer1 = customer1;
                  this.orderId = orderId;
              }
              public Integer getId() {
                  return this.id;
              }
             
              public void setId(Integer id) {
                  this.id = id;
              }

              public Customer1 getCustomer1() {
                  return this.customer1;
              }
             
              public void setCustomer1(Customer1 customer1) {
                  this.customer1 = customer1;
              }

              public String getOrderId() {
                  return this.orderId;
              }
             
              public void setOrderId(String orderId) {
                  this.orderId = orderId;
              }
          }

          配置文件

          Customer1.hbm.xml

          <hibernate-mapping>
              <class name="many_one.Customer1" table="customer1" catalog="test1" >
                  <id name="id" type="integer" >
                      <column name="id" not-null="false"/>
                      <generator class="increment" />
                  </id>
                  <property name="name" type="string">
                      <column name="name" length="20" />
                  </property>
              </class>
          </hibernate-mapping>

           

          Orderc1.hbm.xml

          <hibernate-mapping>
              <class name="many_one.Orderc1" table="orderc1" catalog="test1">
                  <id name="id" type="integer">
                      <column name="id" />
                      <generator class="increment" />
                  </id>
                  <many-to-one name="customer1" class="many_one.Customer1" fetch="select" cascade="save-update">
                      <column name="customer_id" not-null="true" />
                  </many-to-one>
                  <property name="orderId" type="string">
                      <column name="order_id" length="20" />
                  </property>
              </class>
          </hibernate-mapping>

          package many_one;

          import org.hibernate.Session;
          import org.hibernate.SessionFactory;
          import org.hibernate.Transaction;
          import org.hibernate.cfg.Configuration;

          public class tests {

           /**
            * @param args
            */
           public static void main(String[] args) {

            Configuration cfg = new Configuration().configure();
            SessionFactory sf  =cfg.buildSessionFactory();
            Session session =sf.openSession();
            
            Transaction ts = session.beginTransaction();
            
            Customer1 c = new Customer1("c1");
            Orderc1 o = new Orderc1();
            o.setOrderId("23");
            
            o.setCustomer1(c);
            
            session.save(o);
            
            ts.commit();
           
           }


          }

          posted on 2011-03-26 23:37 甜咖啡 閱讀(483) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2011年3月>
          272812345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          統計

          常用鏈接

          留言簿(1)

          我參與的團隊

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 泰顺县| 白银市| 三穗县| 无棣县| 西昌市| 崇仁县| 凤山市| 韶山市| 东乡县| 贵德县| 罗源县| 修水县| 永兴县| 怀化市| 雷州市| 积石山| 察哈| 阿克| 永善县| 荆门市| 紫金县| 临沭县| 兴城市| 新河县| 延安市| 固始县| 肥西县| 巴楚县| 美姑县| 沾化县| 德令哈市| 瑞丽市| 铅山县| 嘉荫县| 琼海市| 临泽县| 辽阳县| 新安县| 略阳县| 曲阳县| 吉安县|