-->
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: Inherited classes referring to the same table / 2x results.
PostPosted: Sun May 14, 2006 6:23 pm 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
Hibernate version: 3.1.2

Name and version of the database you are using: MySQL 4.1.10

I have two classes

Code:
ItemPreview


and

Code:
Item extends ItemPreview
(that has a bunch of mapped collections that the ItemPreview class doesn't have).

Both classes are mapped to the SAME MySQL table using the following mappings.

Mapping documents
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 default-access="property">

  <class name="com.test.data.ItemPreview" table="items" mutable="false">

    <id name="id" column="id">
      <generator class="native"/>
    </id>
     
    <property name="title"
        type="com.test.hibernate.StringModelType"
        column="title" />
   
    <property name="desc"
        type="com.test.hibernate.StringModelType"
        column="descr" />

  </class>
</hibernate-mapping>


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="com.test.data.Item" table="items">

    <id name="id" column="id">
      <generator class="native"/>
    </id>
     
    <property name="title"
        type="com.test.hibernate.StringModelType"
        column="title" />
   
    <property name="desc"
        type="com.test.hibernate.StringModelType"
        column="descr" />   
   
    <property name="pubDate" type="timestamp" column="pub_date" />
       
    <many-to-one name="parent"
        class="com.test.data.Item"
        column="follows_id"
        not-null="false"
        lazy="false" />
       
    <map name="children" lazy="false">
      <key column="follows_id" />
      <map-key column="pub_date" type="timestamp" />
      <one-to-many class="com.test.data.Item" />
    </map>
   
    <map name="comments" lazy="false">
      <key column="id" />
      <map-key column="submit_date" type="timestamp" />
      <one-to-many class="com.test.data.Comment" />
    </map>
   
  </class>

</hibernate-mapping>



My thought process in mapping both entities to the same table was that I thought I could have a more efficient set of queries for getting large groups of item previews and a more comprehensive object for when a user examines a specific Item (and I eagerly -- lazy="false" -- fetch the comments and parent and child entities).

The problem is that in an attempt to get a list of ItemPreview objects I am getting both ItemPreviews and a matching Item in the returned list.

Code between sessionFactory.openSession() and session.close():

Code:
Criteria crit = session.createCriteria(ItemPreview.class);
            crit.addOrder(Order.asc("pubDate"));
            crit.setFirstResult(start);
            crit.setMaxResults(limit);
            List results = crit.list();




If, for example, the limit is set to 10, I get 10 ItemPreview objects followed by 10 Item objects even though I've specified the ItemPreview class in the criteria. This happens using the Criteria as well as a Query in the form "select ip from ItemPreview as ip order by pubDate".

Is this (having Item extend ItemPreview) simply a bad design (I've solved it for the time being by breaking that inheritence and copying the preview fields and accessors to Item)? Or am I missing something obvious in this setup?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 14, 2006 6:40 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Hi!

Quote:
My thought process in mapping both entities to the same table was that I thought I could have a more efficient set of queries for getting large groups of item previews and a more comprehensive object for when a user examines a specific Item (and I eagerly -- lazy="false" -- fetch the comments and parent and child entities).


So ItemPreview has
id,title,desc
and Item has
id,title, desc, pubDate,parent,children,comments

Now... parent,children,comments are relationships so they can be loaded with "lazy loading" so they don't have an impact on you initial loading...

Then... the only justification for you schema is pubDate... and IMHO I think that loading it is not very significant...

There might be cases when you dont want to load all the fields in a class, but this does not seem as one of them... (and IMHO generally whet you class seems to have to many fields, it is because you model is not properly normalized)


Top
 Profile  
 
 Post subject: clarification on proper data layer design?
PostPosted: Tue May 16, 2006 10:04 am 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
luxspes wrote:

Now... parent,children,comments are relationships so they can be loaded with "lazy loading" so they don't have an impact on you initial loading...



That's a good point.

First -- at a risk of going off on a tangent -- I'm a beginner to Hibernate, but I'm instinctively not a huge fan of lazy loading since using a layered development approach I'd like the objects coming back fom the Data Layer to be completely built.

For example, given two sets of interfaces:

Code:
public List< ItemPreview > getAllItems();
public Item getItem(Integer id);


versus...

Code:
// implementation note; the comments and item heirarchy are note loaded in
// theses items
public List< Item > getAllItems();

// this is a fully realized item instance
public Item getItem(Integer id);


It seems to me that the first set of methods is more understandable... Maybe I'm missing something fundamental in the proper layer design?

Back to the original question, however, does anyone know why my initial setup was returning duplicate copies even though only ItemPreview.class was specified in the Criteria creation?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 16, 2006 10:36 am 
Newbie

Joined: Tue May 16, 2006 10:21 am
Posts: 1
Not sure if this is what you are asking, but to have super/sub classes in the same table you need to use the "Table per class hierarchy".

http://www.hibernate.org/hib_docs/v3/re ... leperclass

- Adrian


Top
 Profile  
 
 Post subject: Why didn't I think of that?
PostPosted: Tue May 16, 2006 10:52 am 
Newbie

Joined: Sat May 13, 2006 12:00 pm
Posts: 19
Of course... Thanks.

I guess it didn't cross my mind because the examples are always "siblings" (e.g. "CreditPayment", "CashPayment") instead of a "parent/child" and refer to the parent as an interface rather than a concrete class.

Though I can't imagine there would be any problem in using a concrete ItemPreview as the parent.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.