-->
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: Polymorphic queries
PostPosted: Sun Jul 12, 2009 10:22 am 
Newbie

Joined: Sun Jul 12, 2009 8:54 am
Posts: 1
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


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.