首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

hibernate学习4-联系关系映射-多对一

2012-09-24 
hibernate学习4-关联映射-多对一关联映射分类一对一,一对多(多对一),多对多单向关联和双向关联单向关联 多

hibernate学习4-关联映射-多对一
关联映射分类
一对一,一对多(多对一),多对多

单向关联和双向关联

单向关联 多对一
客户和订单
数据表:
customer(id,name,email)
order(id,name,orderNumber,customerId(外键))

Customer(Integer id,String name,String email)
Order(Integer id,String name,String orderNumber,Customer customer)//不是customerId

映射文件customer.hbm.xml
<?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" >
<hibernate-mapping>
<class name="com.limf.bean.Customer" table="Customer">
<id name="id" column="id">
<generator column="name"></property>
<property name="emai" column="email"></property>

</class>
</hibernate-mapping>

映射文件order.hbm.xml
<?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" >
<hibernate-mapping>
<class name="com.limf.bean.Order" table="Order">
<id name="id" column="id">
<generator column="name"></property>
<property name="orderNumber" column="orderNumber"></property>
   
                  <many-to-one name="customer" table="Customer" lazy="false">



双向关联 多对一
Customer类中增多一个属性Set orders = new HashSet()
映射文件customer.hbm.xml
<?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" >
<hibernate-mapping>
<class name="com.limf.bean.Customer" table="Customer">
<id name="id" column="id">
<generator column="name"></property>
<property name="emai" column="email"></property>

<set name="orders" lazy="false">   <!--不要延迟加载-->
                      <key column="CustomerId"/>  <!--集合外键-->
                      <one-to-many class="com.limf.bean.Order"/>
                  </set>
</class>
</hibernate-mapping>


热点排行