hibernate關系映射(一對多)

          Posted on 2008-08-21 12:00 H2O 閱讀(347) 評論(0)  編輯  收藏 所屬分類: hibernate
          客戶與訂單---一對多
          package com.yz.pojos;

          import java.util.HashSet;
          import java.util.Set;

          /**
           * Customer generated by MyEclipse Persistence Tools
           
          */


          public class Customer implements java.io.Serializable {

              
          // Fields

              
          private Integer cid;

              
          private String name;

              
          private String addr;
              
          //一個客戶可以有多個訂單,一對多關系。但是一個客戶不能有重復的訂單,所以用Set集合,set集合不允許出現重復值
              
          //set集合反映了一個客戶的所有訂單

              
          private Set orderses = new HashSet(0);

              
          // Constructors

              
          /** default constructor */
              
          public Customer() {
              }


              
          /** minimal constructor */
              
          public Customer(String name) {
                  
          this.name = name;
              }


              
          /** full constructor */
              
          public Customer(String name, String addr, Set orderses) {
                  
          this.name = name;
                  
          this.addr = addr;
                  
          this.orderses = orderses;
              }


              
          // Property accessors

              
          public Integer getCid() {
                  
          return this.cid;
              }


              
          public void setCid(Integer cid) {
                  
          this.cid = cid;
              }


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


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


              
          public String getAddr() {
                  
          return this.addr;
              }


              
          public void setAddr(String addr) {
                  
          this.addr = addr;
              }


              
          public Set getOrderses() {
                  
          return this.orderses;
              }


              
          public void setOrderses(Set orderses) {
                  
          this.orderses = orderses;
              }


          }
          <?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"
          >
          <!-- 
              Mapping file autogenerated by MyEclipse Persistence Tools
          -->
          <hibernate-mapping>
              
          <class name="com.yz.pojos.Customer" table="customer" lazy="false" >
                  
          <id name="cid" type="java.lang.Integer">
                      
          <column name="cid" />
                      
          <generator class="native" />
                  
          </id>
                  
          <property name="name" type="java.lang.String">
                      
          <column name="name" length="20" not-null="true" />
                  
          </property>
                  
          <property name="addr" type="java.lang.String">
                      
          <column name="addr" length="50" />
                  
          </property>
                  
          <!-- set集合保存該客戶所有的訂單,通過鍵cid查找訂單 -->
                  
          <set name="orderses"  lazy="true" cascade="delete" inverse="true">
                      
          <key>
                          
          <column name="cid" not-null="true" />
                      
          </key>
                      
          <one-to-many class="com.yz.pojos.Orders" />
                  
          </set>
              
          </class>
          </hibernate-mapping>

          訂單與客戶---多對一
          package com.yz.pojos;

          import java.util.Date;

          /**
           * Orders generated by MyEclipse Persistence Tools
           
          */


          public class Orders implements java.io.Serializable {

              
          // Fields

              
          private Integer oid;
              
          //在多個訂單中,每個訂單只能屬于一個客戶,屬于 多對一關系,所以要告訴每個訂單屬于哪一個客戶對象
              private Customer customer;

              
          private Date odate;

              
          // Constructors

              
          /** default constructor */
              
          public Orders() {
              }


              
          /** full constructor */
              
          public Orders(Customer customer, Date odate) {
                  
          this.customer = customer;
                  
          this.odate = odate;
              }


              
          // Property accessors

              
          public Integer getOid() {
                  
          return this.oid;
              }


              
          public void setOid(Integer oid) {
                  
          this.oid = oid;
              }


              
          public Customer getCustomer() {
                  
          return this.customer;
              }


              
          public void setCustomer(Customer customer) {
                  
          this.customer = customer;
              }


              
          public Date getOdate() {
                  
          return this.odate;
              }


              
          public void setOdate(Date odate) {
                  
          this.odate = odate;
              }


          }

          <?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"
          >
          <!-- 
              Mapping file autogenerated by MyEclipse Persistence Tools
          -->
          <hibernate-mapping>
              
          <class name="com.yz.pojos.Orders" table="orders" catalog="ssh">
                  
          <id name="oid" type="java.lang.Integer">
                      
          <column name="oid" />
                      
          <generator class="native" />
                  
          </id>
                  
          <!-- 訂單與客戶多對一,告訴訂單所關聯的客戶是誰 -->
                  
          <many-to-one name="customer" class="com.yz.pojos.Customer" fetch="select">
                      
          <column name="cid" not-null="true" />
                  
          </many-to-one>
                  
          <property name="odate" type="java.util.Date" insert="true">
                      
          <column name="odate" length="19" not-null="true" />
                  
          </property>
              
          </class>
          </hibernate-mapping>

          posts - 0, comments - 21, trackbacks - 0, articles - 101

          Copyright © H2O

          主站蜘蛛池模板: 蒲江县| 尖扎县| 田阳县| 马公市| 怀来县| 牟定县| 卢湾区| 苍南县| 新民市| 晋江市| 湖南省| 平凉市| 台东县| 民乐县| 石林| 科技| 故城县| 保山市| 商水县| 邹平县| 二手房| 东源县| 乐亭县| 新平| 丹江口市| 武陟县| 宁武县| 比如县| 无为县| 周至县| 山阴县| 明光市| 堆龙德庆县| 麻城市| 金昌市| 花莲市| 申扎县| 临泽县| 防城港市| 油尖旺区| 宜黄县|