hibernate 中一對多關係映射表 客戶和聯繫人

package cn.itcast.domain;java

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;app

public class Customer implements Serializable{dom

    private static final long serialVersionUID = 1L;
    private Long custId;
    private String custName;
    private String custSource;
    private String custIndustry;
    private String custLevel;
    private String custAddress;
    private String custPhone;
    
    private Set<LinkMan> linkMans = new HashSet<LinkMan>();//應該要實例化ide

    public Long getCustId() {
        return custId;
    }
    public void setCustId(Long custId) {
        this.custId = custId;
    }
    public String getCustName() {
        return custName;
    }
    public void setCustName(String custName) {
        this.custName = custName;
    }
    public String getCustSource() {
        return custSource;
    }
    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }
    public String getCustIndustry() {
        return custIndustry;
    }
    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }
    public String getCustLevel() {
        return custLevel;
    }
    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }
    public String getCustAddress() {
        return custAddress;
    }
    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }
    public String getCustPhone() {
        return custPhone;
    }
    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }
    
    public Set<LinkMan> getLinkMans() {
        return linkMans;
    }
    public void setLinkMans(Set<LinkMan> linkMans) {
        this.linkMans = linkMans;
    }
    @Override
    public String toString() {
        return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource
                + ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress
                + ", custPhone=" + custPhone + "]";
    }
}
 this

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="cn.itcast.domain.Customer" table="cst_customer">
        <id name="custId" column="cust_id">
            <generator class="native"></generator>
        </id>
        <property name="custName" column="cust_name"></property>
        <property name="custSource" column="cust_source"></property>
        <property name="custIndustry" column="cust_industry"></property>
        <property name="custLevel" column="cust_level"></property>
        <property name="custAddress" column="cust_address"></property>
        <property name="custPhone" column="cust_phone"></property>
        <!-- 
            set標籤:映射集合屬性
                name屬性:集合屬性的名字
                cascade屬性:配置級聯,save-update:級聯保存或更新
                inverse="true",表示讓一方(主表)放棄外鍵維護權利
         -->
        <set name="linkMans" inverse="true">
            <!-- 
                key標籤:映射外鍵的
                    column屬性:指定外鍵列名    
             -->
            <key column="lkm_customer_id"></key>
            <!-- 
                one-to-many標籤:指定當前實體和對方(聯繫人)是一對多的關係
             -->
            <one-to-many class="cn.itcast.domain.LinkMan"/>
        </set>
    </class>
</hibernate-mapping>.net

package cn.itcast.domain;hibernate

import java.io.Serializable;xml

public class LinkMan implements Serializable {對象

    private static final long serialVersionUID = 1L;
    
    private Long lkmId;
    private String lkmName;
    private String lkmGender;
    private String lkmPhone;
    private String lkmMobile;
    private String lkmEmail;
    private String lkmPosition;
    private String lkmMemo;
    
    private Customer customer;
    
    public Long getLkmId() {
        return lkmId;
    }
    public void setLkmId(Long lkmId) {
        this.lkmId = lkmId;
    }
    public String getLkmName() {
        return lkmName;
    }
    public void setLkmName(String lkmName) {
        this.lkmName = lkmName;
    }
    public String getLkmGender() {
        return lkmGender;
    }
    public void setLkmGender(String lkmGender) {
        this.lkmGender = lkmGender;
    }
    public String getLkmPhone() {
        return lkmPhone;
    }
    public void setLkmPhone(String lkmPhone) {
        this.lkmPhone = lkmPhone;
    }
    public String getLkmMobile() {
        return lkmMobile;
    }
    public void setLkmMobile(String lkmMobile) {
        this.lkmMobile = lkmMobile;
    }
    public String getLkmEmail() {
        return lkmEmail;
    }
    public void setLkmEmail(String lkmEmail) {
        this.lkmEmail = lkmEmail;
    }
    public String getLkmPosition() {
        return lkmPosition;
    }
    public void setLkmPosition(String lkmPosition) {
        this.lkmPosition = lkmPosition;
    }
    public String getLkmMemo() {
        return lkmMemo;
    }
    public void setLkmMemo(String lkmMemo) {
        this.lkmMemo = lkmMemo;
    }
    
    public Customer getCustomer() {
        return customer;
    }
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    @Override
    public String toString() {
        return "LinkMan [lkmId=" + lkmId + ", lkmName=" + lkmName + ", lkmGender=" + lkmGender + ", lkmPhone="
                + lkmPhone + ", lkmMobile=" + lkmMobile + ", lkmEmail=" + lkmEmail + ", lkmPosition=" + lkmPosition
                + ", lkmMemo=" + lkmMemo + ", customer=" + customer + "]";
    }
    get

}

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>     <class name="cn.itcast.domain.LinkMan" table="cst_linkman">         <id name="lkmId" column="lkm_id">             <generator class="native"></generator>         </id>         <property name="lkmName" column="lkm_name"></property>         <property name="lkmGender" column="lkm_gender"></property>         <property name="lkmPhone" column="lkm_phone"></property>         <property name="lkmMobile" column="lkm_mobile"></property>         <property name="lkmEmail" column="lkm_email"></property>         <property name="lkmPosition" column="lkm_position"></property>         <property name="lkmMemo" column="lkm_memo"></property>                  <!--              many-to-one:指定當前實體與對方是多對一的關係                 name屬性:指定當前實體中一方的對象屬性                 class屬性:指定一方的全路徑名                 column屬性:指定外鍵列名    ,須要與一方中指定的外鍵列名一致          -->         <many-to-one name="customer" class="cn.itcast.domain.Customer" column="lkm_customer_id" cascade="delete" lazy="false"></many-to-one>     </class> </hibernate-mapping>

相關文章
相關標籤/搜索