-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Persitence Issue with Joined-Subclass
PostPosted: Fri Mar 30, 2007 11:38 am 
Newbie

Joined: Fri Mar 30, 2007 11:28 am
Posts: 1
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>


Top
 Profile  
 
 Post subject: Same problem, did lame workaround
PostPosted: Fri Mar 30, 2007 6:13 pm 
Newbie

Joined: Sun Jan 21, 2007 6:48 am
Posts: 6
Location: El Cerrito, CA
My problem is almost exactly the same as yours.

For now I did a very lame workaround where I created a dummy class (equivalent to User in your example) that just had the non-null fields needed to create a row in the db. It's mapping was not as a joined-subclass but a regular class.

When creating a new User row (I'll use your table names) that is supposed to reference an existing Contact row, I instead use my dummy class, called UserCreate, to save the row.

This works for me so far, but it's embarrassing and frustrating to have to go to these lengths to get this to work. Both Hibernate books I have (including "The best book about Hibernate ...") talk about joined-subclass from the standpoint of retrieving data. I really like how it works for data retrieval, but it seems broken for the purposes of performing INSERTS. That is, unless you and I and a bunch of other people on this forum are missing something!


Top
 Profile  
 
 Post subject: So much for my lame workaround ...
PostPosted: Sun Apr 01, 2007 10:06 pm 
Newbie

Joined: Sun Jan 21, 2007 6:48 am
Posts: 6
Location: El Cerrito, CA
Well, my lame workaround was just that: lame. It turns out that I could indeed add a child row associated with a pre-existing parent row that way, but the retrieving of the info was broken (Hibernate didn't pick up the child row in the join, apparently because it was now using the UserCreate mapping).

By switching the order of the references in hibernate.cfg.xml I was able to get retrieving to work again, but it broke adding.

I'm going to give up on joined-subclass and go for a composite strategy, where User will hold a reference to its associated Contact.


Top
 Profile  
 
 Post subject: Working using interface rather than inheritance
PostPosted: Mon Apr 02, 2007 2:36 am 
Newbie

Joined: Sun Jan 21, 2007 6:48 am
Posts: 6
Location: El Cerrito, CA
I've got everything working for both inserts and retrieval, but I had to give up on the joined-subclass mapping.

I defined a Contact Interface, which both both User and Contact implemented. User no longer extended Contact.

In the Contact mapping I used a one-to-one mapping to User.

In the User mapping I used a many-to-one mapping to Contact, with unique="true". Although it would make more sense to use a one-to-one mapping on this side as well, it didn't work and Java Persistence with Hibernate recommends the many-to-one/unique mapping even in cases where it's really one-to-one.

Each class holds a reference to the other. Of course, for Contact sometimes the reference to User is null.

After many hours of trial and error I'm glad to have this behind me! Let me know if you need more details.

Good luck!

-Roger


Top
 Profile  
 
 Post subject: *** (sorry, didn't find a way to delete the post)
PostPosted: Sat Apr 28, 2007 7:52 pm 
Newbie

Joined: Tue Mar 20, 2007 4:51 am
Posts: 12
*** (sorry, didn't find a way to delete the post)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.