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.  [ 1 post ] 
Author Message
 Post subject: Polymorph="explicit" and joined-subclass problem
PostPosted: Wed Aug 29, 2007 3:23 am 
Newbie

Joined: Fri Dec 15, 2006 6:39 am
Posts: 3
Hibernate version:
3.2.2

Mapping documents:
Code:
<hibernate-mapping>
   <class name="Assignment" schema="TEST" table="ASGMNT" polymorphism="explicit"  >
      <id column="ID" name="id" type="long" unsaved-value="null">
         <generator class="native" />
      </id>

      <property name="reference" column="REFERENCE" access="field" generated="insert" />

            
      <joined-subclass lazy="true" name="Project" table="PROJECT" schema="TEST">
         <key column="PARENT_ID"/>
         <property name="title" column="TITEL" />
      </joined-subclass>

      <joined-subclass lazy="true" name="Report" table="REPORT" abstract="true" schema="TEST">
         <key column="PARENT_ID"/>
         
         <joined-subclass lazy="true" name="MeanTimeReport" table="TTREPORT" schema="TEST">
            <key column="PARENT_ID"/>            
         </joined-subclass>
         </joined-subclass>
      </joined-subclass>
   </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
Criteria criteria = session.createCriteria(Assignment.class);
                criteria.add(Restrictions.eq("id", 1L));
               criteria.uniqueResult();

The result is a Project instance (subclass). Since I declared Assignment as polymorphism="explicit" and I'm querying an Assignment.class I expected just an Assignment instance, without joining and returning a Project instance.

Since my mapping is very large (which I made simple for this post) the query that was generated joins a lot of tables to determine the runtime instance of the result. This is not acceptable for my project.

What I want to do is to query the Assignment part only. It's all I need. If I wanted the subclass instance (Project in this case), I can provide Project.class as parameter in createCriteria().

Why didn't the polymorphism="explicit" work as expected?
From the hibernate reference:
Code:
Implicit polymorphism means that instances of the class will be returned by a query that names any superclass
or implemented interface or the class and that instances of any subclass of the class will be returned by a query
that names the class itself. Explicit polymorphism means that class instances will be returned only by queries
that explicitly name that class and that queries that name the class will return only instances of subclasses
mapped inside this <class> declaration as a <subclass> or <joined-subclass>. For most purposes the default,
polymorphism="implicit", is appropriate. Explicit polymorphism is useful when two different classes are
mapped to the same table (this allows a "lightweight" class that contains a subset of the table columns).


The problem grows since in my real world application the Assignment has polymorphic relations to itself, where again I don't care if the relation refers to Project or any other subclass. I just need the information of my superclass Assignment.

Hope I made myself clear,
Thanks in advance for any insight you might give me.

Regards,
Andries Inzé


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

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.