7.映射组成关系

来源:互联网 发布:淘宝复制天猫宝贝违规 编辑:程序博客网 时间:2024/06/06 02:18
public class Customer implements java.io.Serializable {<span style="white-space:pre"></span>private Integer id;<span style="white-space:pre"></span>private String name;<span style="white-space:pre"></span>private Address homeAddress;<span style="white-space:pre"></span>private Address comAddress;

配置文件配置如下

<component name="homeAddress" class="cn.itcast.compopent.Address">         <!-- 该配置表示Address是Customer类的一部分 -->         <property name="street" type="string">           <column name="home_street"></column>         </property>                  <property name="city" type="string">           <column name="home_city"></column>         </property>                  <property name="province" type="string">           <column name="home_province"></column>         </property>                  <property name="zipcode" type="string">           <column name="home_zipcode"></column>         </property>      </component>             <component name="comAddress" class="cn.itcast.compopent.Address">         <property name="street" type="string">           <column name="com_street"></column>         </property>                  <property name="city" type="string">           <column name="com_city"></column>         </property>                  <property name="province" type="string">           <column name="com_province"></column>         </property>                  <property name="zipcode" type="string">           <column name="com_zipcode"></column>         </property>      </component>

*    两个javabean对应一张表,组件映射表中字段一般有重复内容,比如说地址,有家庭地址和公司地址

Address ha=c.getHomeAddress();
*   如果homeAddress对应的四个表字段为空  则c.getHomeAddress()为null

public class Address {private String street;private String city;private String province;private String zipcode;//表示Address所属的整体类private Customer customer;
hbm配置文件

<component name="homeAddress" class="cn.itcast.compopent.Address">         <!-- 该配置表示Address是Customer类的一部分 -->         <parent name="customer"/>         <property name="street" type="string">           <column name="home_street"></column>         </property>         <property name="city" type="string">           <column name="home_city"></column>         </property>         <property name="province" type="string">           <column name="home_province"></column>         </property>         <property name="zipcode" type="string">           <column name="home_zipcode"></column>         </property>      </component>
*   在Address类中加上所属类,配置文件加一个parent标签也可以

0 0