关于hiebernate

栏目表Column
c_id是 自增的主键
c_name 栏目名
f_id  父栏目的ID
c_root 它由父栏目的c_root加上四位数字构成的,栏目调用的标记,也是唯一的
 
新闻表NewItem
n_id  自增主键
……
c_root
 
原来设的NewItem关联Column的外键是c_id
但是有与查询麻烦
所以决定把c_root设为外键
但是Column的主键是c_id
开始不知道如何设置
在网上找了半天终于找到思路
使用property-ref
 
 
 
 

有A,B 两表

 A表字段

id (主键)

code(唯一)

 B表字段

id(主键)

a_code(对应a表中的code)

如何使用 配置映射关系?

自己搞定了,
使用property-ref.
copy一段文档:

23.4.5. Associations on alternate keys
<class name="Person">
    
    <id name="id">
        <generator class="hilo"/>
    </id>
    
    <property name="name" length="100"/>
    
    <one-to-one name="address"
        property-ref="person"
        cascade="all"
        fetch="join"/>
    
    <set name="accounts"
        inverse="true">
        <key column="userId"
            property-ref="userId"/>
        <one-to-many class="Account"/>
    </set>
    
    <property name="userId" length="8"/>

</class>

<class name="Address">

    <id name="id">
        <generator class="hilo"/>
    </id>

    <property name="address" length="300"/>
    <property name="zip" length="5"/>
    <property name="country" length="25"/>
    <many-to-one name="person" unique="true" not-null="true"/>

</class>

<class name="Account">
    <id name="accountId" length="32">
        <generator class="uuid"/>
    </id>
    
    <many-to-one name="user"
        column="userId"
        property-ref="userId"/>
    
    <property name="type" not-null="true"/>
    
</class>

http://www2.matrix.org.cn/thread.shtml?topicId=4d5d9475-1962-11dc-8168-ef01faefba17&forumId=23

自己试了一下

终于解决

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="link.cms.models">
    <class name="NewItem" table="cms_newitem">
        <id name="n_id" column="n_id">
            <generator class="identity"/>
        </id>
        <property name="n_title" not-null="true" column="n_title" type="string"/>
        <property name="n_author" not-null="true" column="n_author" type="string"/>        
        <property name="n_context" not-null="true" column="n_context" type="string"/>
        <property name="n_time" not-null="true" column="n_time" type="timestamp"/>
        <many-to-one name="column" column="c_root"
            class="link.cms.models.Column" lazy="false" not-null="true"
            unique-key="c_root" property-ref="c_root" />
    </class>
</hibernate-mapping>

 

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="link.cms.models">
    <class name="Column" table="cms_column">
        <id name="c_id" column="c_id">
                <generator class="identity"/>
        </id>
        <property name="c_name" not-null="true" column="c_name" type="string"/>
        <property name="c_root" not-null="true" column="c_root"
            type="string" unique="true" />
        <many-to-one name="father" column="f_id" class="link.cms.models.Column" lazy="false"/>
        <set name="children" lazy="false" cascade="delete"
            order-by="c_id">
            <key column="f_id" />
            <one-to-many class="link.cms.models.Column" />
        </set>
        <set name="newItems" order-by="n_id" cascade="delete"
            inverse="true">
            <key column="c_root" property-ref="c_root"></key>
            <one-to-many class="link.cms.models.NewItem"></one-to-many>
        </set>
    </class>    
</hibernate-mapping>

你可能感兴趣的:(C++,c,Hibernate,cms,C#)