Chinaunix首页 | 论坛 | 博客
  • 博客访问: 735819
  • 博文数量: 60
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 2090
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-27 12:33
文章分类

全部博文(60)

文章存档

2008年(60)

我的朋友

分类: Java

2008-03-26 15:57:04

栏目表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

    
    
        
    

    
    
    
            property-ref="person"
        cascade="all"
        fetch="join"/>
    
            inverse="true">
                    property-ref="userId"/>
        

    
    





    
        
    


    
    
    
    




    
        
    

    
            column="userId"
        property-ref="userId"/>
    
    
    

自己试了一下

终于解决

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "">
<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"
        "">
<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>

阅读(1000) | 评论(0) | 转发(0) |
0

上一篇:JAVA操作COOKIE

下一篇:java正则表达式

给主人留下些什么吧!~~