Hi all,
I have a problem with persisting an object hierarchy. I have 2 tables Contacts and Users. I have used joined-subclass approach to model these tables where Users is a subclass of Contacts. Not all contacts are users of the application. I have a association from a another class (let's say List) which will have a collection of Contacts. The problem I am seeing is that when I add a collection of Contacts to List and persist List, hibernate not only persists lists and contacts but also inserts new rows in Users table which is not what we want. Can someone explain me why this is happening and how I can avoid creating Users since List do not have anything to do with users.
Here is the mapping file
Code:
<?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="Contacts" table="CONTACTS_TBL">
<id name="uid" column="CONTACT_UID">
<generator class="increment" />
</id>
<property name="contactEmail" column="CONTACT_EMAIL"
type="string" />
<property name="createDate" column="CREATE_DATE"
type="java.util.Date" />
<property name="updatedBy" column="UPDATED_BY_EMAIL_ID"
type="string" />
<property name="updateDate" column="UPDATE_DATE"
type="java.util.Date" />
<set name="lists" table="LIST_CONTACT_TBL" inverse="true"
lazy="true" cascade="save-update,persist" fetch="join">
<key column="CONTACT_UID" not-null="true" />
<many-to-many class="List" column="LIST_UID" />
</set>
<joined-subclass name="User" table="USER_TBL" >
<key column="USER_UID" />
<many-to-one name="role" column="ROLE_ID" fetch="join" class="ListRole" lazy="false"/>
</joined-subclass>
</class>
<query name="findContactsByEmail">
from Contacts as c where c.contactEmail like :emailId
</query>
<query name="findAllContactsByEmailList">
from Contacts as c left outer join fetch c.lists where
c.contactEmail in (:emailList)
</query>
<query name="findUserByEmail">
from User as u where u.contactEmail =:emailId
</query>
</hibernate-mapping>