Shekhar
Shekhar

Reputation: 11788

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 :

<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? Please help me. If you need more details then please let me know.

Thanks in Advance !!!

Upvotes: 0

Views: 407

Answers (1)

powerMicha
powerMicha

Reputation: 2773

You will have to map Entity as own class first.

<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.

For further documentation see: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/collections.html#collections-ofvalues

Upvotes: 1

Related Questions