-->
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.  [ 3 posts ] 
Author Message
 Post subject: Criteria and class property
PostPosted: Thu Aug 23, 2007 6:27 am 
Newbie

Joined: Thu Aug 23, 2007 6:12 am
Posts: 2
Hibernate version: 3.2.0cr2

Hi all,

I'm trying to use Hibernate criteria api for getting query results based on the class type. I'm using table-per-subclass inheritance strategy.

If I get the results using the HQL, it works. The query looks like this:

select t from MyBaseClass t where t.class=MyChildClass

However, it doesn't work if I try to accomplish the same thing using criteria api with the following code:

Code:
Criteria criteria = session.createCriteria(MyBaseClass.class);
criteria.add(Restrictions.eq("class", MyChildClass.class));
criteria.list();


When I execute the above code I get ClassCastException:

Code:
Caused by: java.lang.ClassCastException: java.lang.Class
        at org.hibernate.type.IntegerType.set(IntegerType.java:41)
        at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:83)
        at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:65)
        at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:
        at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:157
        at org.hibernate.loader.Loader.doQuery(Loader.java:661)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollection
        at org.hibernate.loader.Loader.doList(Loader.java:2145)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029
        at org.hibernate.loader.Loader.list(Loader.java:2024)
        at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1562)
        at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)


Am I using the Criteria API correctly? Because there is no automatic conversion of the criteria value to the appropriate discriminator alias.

Any idea would be greatly appreciated.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 23, 2007 7:49 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

send your mapping files


Amila


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 2:48 pm 
Newbie

Joined: Thu Aug 23, 2007 6:12 am
Posts: 2
Hi Amila,

Mappings are something like this:

for base

Code:
<hibernate-mapping>
    <class name="MyBaseClass" table="PARENT">
        <id name="id" column="PARENT_ID">
            <generator class="native" />
        </id>

        <property name="name" column="NAME" type="java.lang.String" length="50"/>
    </class>
   
</hibernate-mapping>


and for child

Code:

<hibernate-mapping>
    <joined-subclass name="MyChildClass" extends="MyBaseClass" table="CHILD">
        <key column="CHILD_ID" ></key>
        <property name="description" column="DESCRIPTION" type="java.lang.String"/>
    </joined-subclass>
   
</hibernate-mapping>


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

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.