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?