Hi,
Hibernate version: 3.2.3.ga
I have the following object model in my system:
Interface A
|- B implements A
|- C extends B
|- D extends B
B, C and D are all mapped in a single hbm file. Here is the mapping (I removed most fields for brevity):
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<import class="com.myapp.A" rename="A"/>
<class
name="com.myapp.B"
table="composition_types" lazy="false" abstract="true" discriminator-value="1">
<subclass
name="com.myapp.C"
discriminator-value="2" />
<subclass
name="com.myapp.D"
discriminator-value="3">
</subclass>
</class>
</hibernate-mapping>
Now, when I try run the following HQL query:
Code:
sessionFactory.getCurrentSession().createQuery("FROM A WHERE id=:id").setSerializable("id", id).uniqueResult()
I get this exception: org.hibernate.hql.ast.QuerySyntaxException: A is not mapped [FROM A WHERE id=:id]
And when I try to simply load the object by:
Code:
sessionFactory.getCurrentSession().load(A.class, id)
I get this exception: org.hibernate.MappingException: Unknown entity: com.myapp.A
However, when I run the following code:
Code:
sessionFactory.getCurrentSession().createCriteria(A.class).add(Expression.idEq(id)).uniqueResult();
everything works as expected!!Can anyone explain to me why does the Criteria API work in this case, while the other invocations fail?
Regards,
Shahar