单向one-to-many 和 双向one-to-many

来源:互联网 发布:mac查看ipod隐藏文件 编辑:程序博客网 时间:2024/06/06 02:12

一、单向one-to-many

单向的one(A表)-to-many(B表)是A表的一条数据可对应B表的多条数据,能从B表中能对应查找出相关的A表中关联的信息,而A表不能查出关联B表中的的信息,一般情况下外键在B表中,只有在A表的***.hbm.xml中只需要添加类的属性,没有关联属性,B表的***.hbm.xml中除了添加类的属性,还要添加:

 <many-to-one name="B表的关联属性" class="A表的地址">            <column name="B表的外键名" />        </many-to-one>
例:

District.java(A表的创建类)

public class District {private Integer districtId;private String districtName;public Integer getDistrictId() {return districtId;}public void setDistrictId(Integer districtId) {this.districtId = districtId;}public String getDistrictName() {return districtName;}public void setDistrictName(String districtName) {this.districtName = districtName;}}

Street.java(B表的创建类)

public class Street {private Integer streetId;private District district;private String streetName;public Integer getStreetId() {return streetId;}public void setStreetId(Integer streetId) {this.streetId = streetId;}public District getDistrict() {return district;}public void setDistrict(District district) {this.district = district;}public String getStreetName() {return streetName;}public void setStreetName(String streetName) {this.streetName = streetName;}}
 

District.hbm.xml(A表的映射文件)

<hibernate-mapping>    <class name="com.isoft.model.District" table="t_district" lazy="true">        <id name="districtId" type="java.lang.Integer">            <column name="t_district_id" />            <generator class="assigned" />        </id>              <property name="districtName" type="java.lang.String">            <column name="t_district_name" length="50" />        </property>    </class></hibernate-mapping>

Street.hbm.xml(B表的映射文件)
<hibernate-mapping>    <class name="com.isoft.model.Street" table="t_street" lazy="true">        <id name="streetId" type="java.lang.Integer">            <column name="t_street_id"/>            <generator class="assigned" />        </id>        <many-to-one name="district" class="com.isoft.model.District">            <column name="t_district_id" />        </many-to-one>        <property name="streetName" type="java.lang.String">            <column name="t_street_name" length="50" />        </property>    </class></hibernate-mapping>


二、双向one-to-many

           双向one(A表)-to-many(B表)是A表的一条数据可对应B表的多条数据,能从B表中能对应查找出相关的A表中关联的信息,而A表也能查出关联B表中的的信息。两个表的类中都有外键属性,但是表中的外键还在B表中。

A表中除了本类属性还要添加:

<set name="在A表类中定义的B表类的属性名">            <key>                <column name="B表的外键"/>            </key>            <one-to-many class="B表类的地址" /></set>
B表中除了本类属性还要添加:

<many-to-one name="在B表类中定义A表的属性" class="A表类的地址" column="外键的列名"></many-to-one>
例:

TDepartment.java(A表的创建类)

public class TDepartment implements java.io.Serializable {private String TDepid;private String TDname;private Set<TEmployee> TEmployees = new HashSet<TEmployee>(0);
public String getTDepid() {return TDepid;}public void setTDepid(String tDepid) {TDepid = tDepid;}public String getTDname() {return TDname;}public void setTDname(String tDname) {TDname = tDname;}public Set<TEmployee> getTEmployees() {return TEmployees;}public void setTEmployees(Set<TEmployee> tEmployees) {TEmployees = tEmployees;}}

(B表的创建类)

public class TEmployee implements java.io.Serializable {private String TId;private TDepartment TDepartment;private String TName;public String getTId() {return TId;}public void setTId(String tId) {TId = tId;}public TDepartment getTDepartment() {return TDepartment;}public void setTDepartment(TDepartment tDepartment) {TDepartment = tDepartment;}public String getTName() {return TName;}public void setTName(String tName) {TName = tName;}}

TDepartment.hbm.xml(A表的映射文件)

<hibernate-mapping>    <class name="com.tjtc.test.TDepartment" table="t_department" catalog="shujuk">        <id name="TDepid" type="string">            <column name="t_depid" length="50" />            <generator class="assigned" />        </id>        <property name="TDname" type="string">            <column name="t_dname" length="50" />        </property>        <set name="TEmployees">//连接B表的外键实现双向            <key>                <column name="t_depid"/>            </key>            <one-to-many class="com.tjtc.test.TEmployee" />        </set>    </class></hibernate-mapping>

TEmployee.hbm.xml(B表的映射文件)
<hibernate-mapping>    <class name="com.tjtc.test.TEmployee" table="t_employee" catalog="shujuk">        <id name="TId" type="string">            <column name="t_id" length="50" />            <generator class="assigned" />        </id>        <many-to-one name="TDepartment" class="com.tjtc.test.TDepartment" column="t_depid">              </many-to-one>        <property name="TName" type="string">            <column name="t_name" length="50" />        </property>    </class></hibernate-mapping>


原创粉丝点击