休眠组合键 [英] Composite Key with Hibernate

查看:60
本文介绍了休眠组合键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了生成下一个SQL代码:

In order to generate the next SQL code:

create table users (
    user_name varchar(15) not null primary key, 
    user_pass varchar(15) not null);

create table user_roles(
    username varchar(15) not null,
    role_name varchar(15) not null, 
    primary key(usernmae, rolename)
);

您可以使用以下代码:

 <?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>
    <class name="databaselayer.users.UserDB" table="users">
        <id name="username" type="string" column="user_name">
                <meta attribute="scope-set">public</meta>      
        </id>    
        <property name="password" type="string" column="user_pass"  not-null="true"/>
        <set name="roles" cascade="save-update" inverse="true">
                <key column="user_name"/>
                <one-to-many class="databaselayer.users.RoleDB"/>
        </set>                       
    </class>
   <class name="databaselayer.users.RoleDB" table="user_roles">
        <composite-id>
            <key-many-to-one name="username" class="databaselayer.users.UserDB" column="user_name"/>
            <key-property name="role" type="string" column="role_name"/>
        </composite-id>             
    </class>          
</hibernate-mapping>

您可能需要在类路径中包含类以及hbm映射文件.
接下来,我发布我的Ant模式目标:
注意 $ {basedir}/build/WEB-INF/classes包含* .class和* .hbm.xml文件.

You may need to have your classes as well as hbm mapping files in your classpath.
Next I post my Ant schema target:
Note ${basedir}/build/WEB-INF/classes contains both *.class and *.hbm.xml files.

<target name="schema" description="Generate DB schema from the O/R mapping files">
    <hibernatetool destdir="${basedir}">            
        <classpath path="${basedir}/build/WEB-INF/classes">
            <fileset dir="${basedir}/build/WEB-INF/classes">
            <include name="**/*"/>
        </fileset>              
        </classpath>
        <configuration configurationfile="${basedir}/hibernate.cfg.xml"/>
        <hbm2ddl 
            drop="true" 
            create="true"
            export="true"
            outputfilename="libamo2-ddl.sql"
            delimiter=";"
            format="true"/>       
    </hibernatetool>
</target>

Hibernate文档中的相关链接

复合ID
组件作为复合ID

推荐答案

在休眠文档中查找'composite-id'元素.以下内容可能至少会给您一个意图-将您的 id 元素替换为:

Look in the hibernate docs for the 'composite-id' element. The following may at least give you the intent -- replace your id element with:

<composite-id>
    <key-property name="username"/>
    <key-property name="role"/>
</composite-id>

请注意,强烈建议 将ID设置为一个单独的类(组件"),该类实现hashCode并正确地等于它,并且是可序列化的.否则,您将只有非常尴尬的方法来使用 session.get() session.load()查找对象.

Note that it's strongly recommended to instead make your ID a separate class ('component') that implements hashCode and equals properly, and that is Serializable. Otherwise you'll have only very awkward ways to lookup your object using session.get() or session.load().

这篇关于休眠组合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆