首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

hibernate投射文件set key one-to-many

2013-11-02 
hibernate映射文件set key one-to-manyhibernate映射文件set key one-to-many的配置。POJOs如下:Customer类

hibernate映射文件set key one-to-many

hibernate映射文件set key one-to-many的配置。POJOs如下:Customer类------>customer表   Order类对应---------->orders表 customer(1)<--------------->(n)orderpublicclassCustomer{privateString id;privateString username;privateString password;privateTimestamp registerTime;privateintage;privateSet<Order> orders = newHashSet<Order>();/*setter and getter method*/}publicclassOrder{privateString id;privateString orderNumber;privateintbalance;privateCustomer customer;/*setter and getter method*/ Set集合映射:Hibernate为集合映射提供了专用的标签元素,Set集合映射,就使用<set>标签表示:<?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.suxiaolei.hibernate.pojos.Customer"table="customer">            <!--主键设置 -->            <id name="id"type="string">                <column name="id"></column>                <generator class="uuid"></generator>            </id>                        <!--属性设置 -->            <property name="username"column="username"type="string"></property>            <property name="password"column="password"type="string"></property>            <property name="age"column="age"type="integer"></property>            <property name="registerTime"column="register_time"type="timestamp"></property>                        <set name="orders"inverse="true"cascade="all">                <key column="customer_id"></key>                <one-to-many class="com.suxiaolei.hibernate.pojos.Order"/>            </set>                </class>    </hibernate-mapping> <set>标签中的"name"属性表示customer对象中关系集合的属性名,"inverse"与"cascade"属性说明(参考这里)。在数据库中表示"一对多"的关系是通过外键关联的方式实现的,"多方"通过持有"一方"的主键值来确定关系,怎么持有"一方"的主键值?"多方"将使用一列来存储"一方"的主键值,然后将此列作为外键列参照"一方"的主键列。所以使用Hibernate开发时需要将两表的关系列(外键列)告诉Hibernate,<key column="customer_id"></key>就是完成这个工作的,Hibernate就能根据 "customer_id"列取出关联信息。例如:从customer表中取出一条记录后,Hibernate会根据该customer记录的主键值再从order表中查找"custom_id"列,取出值相等的记录,然后组装到Customer对象中的set集合属性中,反之亦然。因为取出来的记录(只是一些零碎的值,还没有组装成对象)需要存放到Set集合中,所以要告诉Hibernate在Set集合里面能放什么类型的数据。<one-to-many>这个标签就是完成这个工作的,"class"属性是指定这个这个Set集合里面元素的类型。 <?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.suxiaolei.hibernate.pojos.Order"table="orders"><id name="id"type="string"><column name="id"></column><generator class="uuid"></generator></id><property name="orderNumber"column="orderNumber"type="string"></property><property name="balance"column="balance"type="integer"></property><many-to-one name="customer"class="com.suxiaolei.hibernate.pojos.Customer"><column name="customer_id"></column></many-to-one></class></hibernate-mapping><many-to-one>标签是设置"一对多"关系中的"多方"的,name指定了哪一个属性是关系属性,"class"指定了关系属性的类型(也指定了与哪一个表关联), "column"属性是指定这个关联属性是按照"customer_id"列的值,在customer表中查询获得的。

热点排行