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?