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é