-->
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: HQL issue with Criteria and association
PostPosted: Fri Sep 26, 2008 4:10 am 
Newbie

Joined: Fri Sep 26, 2008 3:29 am
Posts: 1
Hi

I am a newbie to Hibernate trying to create an integrated JSF/Spring/Hibernate app.

My mapping file is:
Code:
<hibernate-mapping auto-import="true" default-lazy="false">
    <class name="net.time2know.classes.Category" >
        <id name="id" type="int" column="categoryId" >
            <generator class="increment"/>
        </id>
         <property name = "categoryName" type="string"/>
        <list name="images" inverse="true" cascade="all,delete-orphan">
            <key column="categoryId"/>
            <index column="imageId"/>
            <one-to-many class="net.time2know.classes.Image"/>
        </list>
    </class>
    <class name="net.time2know.classes.Image">
        <id name="id" type="int" column="imageId">
            <generator class="increment"/>
        </id>
        <property name="imageTitle" type="string"/>
        <property name="imageUrl" type="string"/>
         <property name="userName" type="string"/>
        <many-to-one name="category" class="net.time2know.classes.Category" column="categoryId"  />
    </class>
</hibernate-mapping>


Category has an images property of type List and categoryName of type String.
Image has a category property of type Category and the other regular persistent properties.

Now I am trying to execute the following Criteria in a Spring DAO:

Code:
Criteria criteria1 = session.createCriteria(Image.class);
Criteria imgCriteria1 = criteria1.createCriteria("category");
criteria1.add(Restrictions.like("imageTitle", "%" + imageTitle + "%"));
this.resultList = imgCriteria1.list();



This results in
Code:
java.lang.ClassCastException: net.time2know.classes.Image cannot be cast to net.time2know.classes.Category


The generated SQL is:

Code:
Hibernate: select this_.imageId as imageId1_1_, this_.imageTitle as imageTitle1_1_, this_.imageUrl as imageUrl1_1_, this_.userName as userName1_1_, this_.categoryId as categoryId1_1_, category1_.categoryId as categoryId0_0_, category1_.categoryName as category2_0_0_ from Image this_ inner join Category category1_ on this_.categoryId=category1_.categoryId where this_.imageTitle like ?

Hibernate: select images0_.categoryId as categoryId1_, images0_.imageId as imageId1_, images0_.imageId as imageId1_0_, images0_.imageTitle as imageTitle1_0_, images0_.imageUrl as imageUrl1_0_, images0_.userName as userName1_0_, images0_.categoryId as categoryId1_0_ from Image images0_ where images0_.categoryId=?


Where am I going wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 10:43 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Code:
this.resultList = imgCriteria1.list();


Is "this.resultList" a list of Images?

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2008 1:57 pm 
Beginner
Beginner

Joined: Mon Jul 07, 2008 8:07 pm
Posts: 42
If you're using Spring and Hibernate, why not use the AnnotatedSessionFactoryBean and the HibernateDaoSupportClass? That gets all of the busy work out of the way and allows you to get more done.
Just a thought.


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.