简体   繁体   English

如何使用以多种不同方式使用的休眠方式来持久化一个类?

[英]How to persist a class using hibernate which is used in many different ways?

I am new to hibernate world, still learning it. 我是新来的冬眠世界,还在学习中。 I am trying to persist one class in database using hibernate. 我正在尝试使用休眠方式在数据库中保留一个类。 Objects of this class can have different meaning in its client classes. 此类的对象在其客户端类中可以具有不同的含义。 Following is the class definition which I want to persist : 以下是我要保留的类定义:

class Entity {
    int id;
    String name;
}

class ClientClass {
    int clientClassID;
    List<Entity> employerList;
    List<Entity> clientList;
}

Mapping file for ClientClass is as follows : ClientClass映射文件如下:

<hibernate-mapping package="com.foo.bar.model">
    <class name="ClientClass" table="CLIENTS">
        <id name="clientClassID" column="CLIENT_ID" type="integer"
            unsaved-value="0">
            <generator class="native" />
        </id>
        <set name="clientList" table="PROJECT_CLIENTS" cascade="all">
            <key column="CLIENT_ID" />
            <many-to-many column="CLIENT_ID" class="Entity" />
        </set>
        <property name="description" type="string" length="1000" />
        <set name="employerList" table="PROJECT_EMPLOYERS" cascade="all">
            <key column="CLIENT_ID" />
            <many-to-many column="EMPLOYER_ID" class="Entity" />
        </set>
         </class>
</hibernate-mapping>

I am not able to understand how to write mappings for Entity class? 我不明白如何为Entity类编写映射? Please help me. 请帮我。 If you need more details then please let me know. 如果您需要更多详细信息,请告诉我。

Thanks in Advance !!! 提前致谢 !!!

You will have to map Entity as own class first. 您将必须首先将Entity映射为自己的类。

<class name="Entity" table="ENTITY">
    <id name="id" column="id" type="integer"
        unsaved-value="0">
        <generator class="native" />
    </id>
    <property name="name" type="string" />
</class>

Then you can add it as Many-To-Many association in your other classes. 然后,您可以将其添加为其他类中的“多对多”关联。 I do not really understand how your mapping on EMPLOYER_NAME and CLIENT_ID should work. 我不太了解您在EMPLOYER_NAMECLIENT_ID上的映射应如何工作。

For further documentation see: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html#collections-ofvalues 有关更多文档,请参见: http : //docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html#collections-ofvalues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM