0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot for answer. If I do not want to create ENTITY table, then what should I do? Instead of ENTITY class, I want to create tables named CLIENT_LIST or EMPLOYER_LIST which have id and name columns.
Is it still many-to-many then? I think it is one-to-many but that's not the solution. I would create two further classes that extend Entity and map them. There is no possibility to map one entity on two tables.
okay, but then I have to create those subclasses unnecessarily just because hibernate cant do this. Actually earlier I had created different database schema but I was facing some problem in it. So I changed it to the one like above. I have posted question on SO. Here is the link for it : stackoverflow.com/questions/7964417/… . Please have a look at it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.