hibernate關(guān)系映射(一對(duì)多)

          Posted on 2008-08-21 12:00 H2O 閱讀(349) 評(píng)論(0)  編輯  收藏 所屬分類: hibernate
          客戶與訂單---一對(duì)多
          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;
              
          //一個(gè)客戶可以有多個(gè)訂單,一對(duì)多關(guān)系。但是一個(gè)客戶不能有重復(fù)的訂單,所以用Set集合,set集合不允許出現(xiàn)重復(fù)值
              
          //set集合反映了一個(gè)客戶的所有訂單

              
          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>

          訂單與客戶---多對(duì)一
          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;
              
          //在多個(gè)訂單中,每個(gè)訂單只能屬于一個(gè)客戶,屬于 多對(duì)一關(guān)系,所以要告訴每個(gè)訂單屬于哪一個(gè)客戶對(duì)象
              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>
                  
          <!-- 訂單與客戶多對(duì)一,告訴訂單所關(guān)聯(lián)的客戶是誰 -->
                  
          <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

          主站蜘蛛池模板: 大足县| 阿拉善左旗| 天水市| 波密县| 安康市| 多伦县| 崇文区| 邵武市| 湖北省| 灯塔市| 枝江市| 鸡西市| 团风县| 渝北区| 乌苏市| 南宫市| 依兰县| 黔江区| 沧州市| 响水县| 富锦市| 峨边| 江都市| 锡林浩特市| 大化| 金堂县| 浮梁县| 西藏| 察雅县| 福安市| 唐海县| 新和县| 营山县| 潮安县| 定陶县| 门头沟区| 湛江市| 三江| 通许县| 东乡族自治县| 定结县|