关于hibernat中<hibernate-mapping>如何关联两个类

J2EE 码拜 6年前 (2015-04-18) 540次浏览 0个评论
 

请问下在hibernate中两个类是如何关联的呢?例如:
<?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=”balance” column=”balance” type=”integer”></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>

<?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=”cost” column=”cost” type=”integer”></property>
            
            <many-to-one name=”customer” class=”com.suxiaolei.hibernate.pojos.Customer” 
                         column=”customer_id” cascade=”save-update”>
            </many-to-one>        
        </class>
    </hibernate-mapping>
上面的代码是我在网页中copy的一个例子,第一个顾客,第二个是订单,一对多关系,在其中<set>标签中的name属性是代表什么?<key>中column的值又是怎么设置的?还有就是这个两个类(customer、order)类又是如何建立关联的呢?谢谢哈!

关于hibernat中<hibernate-mapping>如何关联两个类
20分
 <many-to-one name=”customer” class=”com.suxiaolei.hibernate.pojos.Customer” 
                         column=”customer_id” cascade=”save-update”>
            </many-to-one>    
<set name=”orders” inverse=”true” cascade=”all”>
                <key column=”customer_id” ></key>
                <one-to-many class=”com.suxiaolei.hibernate.pojos.Order”/>
            </set>

就是通过这两个配置关联的    一个多对一 一个一对多   <key column=”customer_id” ></key> column=”customer_id” cascade=”save-update”>这两个是关联的字段 

关于hibernat中<hibernate-mapping>如何关联两个类
那<key> 元素中column=“customer_id”中这个”customer_id”如从哪儿来的呢?
关于hibernat中<hibernate-mapping>如何关联两个类
引用 1 楼 sizhouzhou 的回复:

 <many-to-one name=”customer” class=”com.suxiaolei.hibernate.pojos.Customer” 
                         column=”customer_id” cascade=”save-update”>
            </many-to-one>    
<set name=”orders” inverse=”true” cascade=”all”>
                <key column=”customer_id” ></key>
                <one-to-many class=”com.suxiaolei.hibernate.pojos.Order”/>
            </set>

就是通过这两个配置关联的    一个多对一 一个一对多   <key column=”customer_id” ></key> column=”customer_id” cascade=”save-update”>这两个是关联的字段 

那<key> 元素中column=“customer_id”中这个”customer_id”如从哪儿来的呢?

关于hibernat中<hibernate-mapping>如何关联两个类
引用 3 楼 baidu_17449005 的回复:
Quote: 引用 1 楼 sizhouzhou 的回复:

 <many-to-one name=”customer” class=”com.suxiaolei.hibernate.pojos.Customer” 
                         column=”customer_id” cascade=”save-update”>
            </many-to-one>    
<set name=”orders” inverse=”true” cascade=”all”>
                <key column=”customer_id” ></key>
                <one-to-many class=”com.suxiaolei.hibernate.pojos.Order”/>
            </set>

就是通过这两个配置关联的    一个多对一 一个一对多   <key column=”customer_id” ></key> column=”customer_id” cascade=”save-update”>这两个是关联的字段 

那<key> 元素中column=“customer_id”中这个”customer_id”如从哪儿来的呢?

引用 3 楼 baidu_17449005 的回复:
Quote: 引用 1 楼 sizhouzhou 的回复:

 <many-to-one name=”customer” class=”com.suxiaolei.hibernate.pojos.Customer” 
                         column=”customer_id” cascade=”save-update”>
            </many-to-one>    
<set name=”orders” inverse=”true” cascade=”all”>
                <key column=”customer_id” ></key>
                <one-to-many class=”com.suxiaolei.hibernate.pojos.Order”/>
            </set>

就是通过这两个配置关联的    一个多对一 一个一对多   <key column=”customer_id” ></key> column=”customer_id” cascade=”save-update”>这两个是关联的字段 

那<key> 元素中column=“customer_id”中这个”customer_id”如从哪儿来的呢?

引用 3 楼 baidu_17449005 的回复:
Quote: 引用 1 楼 sizhouzhou 的回复:

 <many-to-one name=”customer” class=”com.suxiaolei.hibernate.pojos.Customer” 
                         column=”customer_id” cascade=”save-update”>
            </many-to-one>    
<set name=”orders” inverse=”true” cascade=”all”>
                <key column=”customer_id” ></key>
                <one-to-many class=”com.suxiaolei.hibernate.pojos.Order”/>
            </set>

就是通过这两个配置关联的    一个多对一 一个一对多   <key column=”customer_id” ></key> column=”customer_id” cascade=”save-update”>这两个是关联的字段 

那<key> 元素中column=“customer_id”中这个”customer_id”如从哪儿来的呢?

这个是表的字段名  也就是id主键

关于hibernat中<hibernate-mapping>如何关联两个类
引用 4 楼 sizhouzhou 的回复:
Quote: 引用 3 楼 baidu_17449005 的回复:
Quote: 引用 1 楼 sizhouzhou 的回复:

 <many-to-one name=”customer” class=”com.suxiaolei.hibernate.pojos.Customer” 
                         column=”customer_id” cascade=”save-update”>
            </many-to-one>    
<set name=”orders” inverse=”true” cascade=”all”>
                <key column=”customer_id” ></key>
                <one-to-many class=”com.suxiaolei.hibernate.pojos.Order”/>
            </set>

就是通过这两个配置关联的    一个多对一 一个一对多   <key column=”customer_id” ></key> column=”customer_id” cascade=”save-update”>这两个是关联的字段 

那<key> 元素中column=“customer_id”中这个”customer_id”如从哪儿来的呢?

引用 3 楼 baidu_17449005 的回复:
Quote: 引用 1 楼 sizhouzhou 的回复:

 <many-to-one name=”customer” class=”com.suxiaolei.hibernate.pojos.Customer” 
                         column=”customer_id” cascade=”save-update”>
            </many-to-one>    
<set name=”orders” inverse=”true” cascade=”all”>
                <key column=”customer_id” ></key>
                <one-to-many class=”com.suxiaolei.hibernate.pojos.Order”/>
            </set>

就是通过这两个配置关联的    一个多对一 一个一对多   <key column=”customer_id” ></key> column=”customer_id” cascade=”save-update”>这两个是关联的字段 

那<key> 元素中column=“customer_id”中这个”customer_id”如从哪儿来的呢?

引用 3 楼 baidu_17449005 的回复:
Quote: 引用 1 楼 sizhouzhou 的回复:

 <many-to-one name=”customer” class=”com.suxiaolei.hibernate.pojos.Customer” 
                         column=”customer_id” cascade=”save-update”>
            </many-to-one>    
<set name=”orders” inverse=”true” cascade=”all”>
                <key column=”customer_id” ></key>
                <one-to-many class=”com.suxiaolei.hibernate.pojos.Order”/>
            </set>

就是通过这两个配置关联的    一个多对一 一个一对多   <key column=”customer_id” ></key> column=”customer_id” cascade=”save-update”>这两个是关联的字段 

那<key> 元素中column=“customer_id”中这个”customer_id”如从哪儿来的呢?

这个是表的字段名  也就是id主键

那也就是说主键的字段名是:表名_<id>中定义的列名,是么?就像上面的表名是:customer,id元素中定义的列名为:id,最后得出主键名为:customer_id?


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于hibernat中<hibernate-mapping>如何关联两个类
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!