Hello everyone,
I have two classes:
Category and CategoryType
Code:
public class Category implements Serializable, Comparable<Object> { private int id = 0;
private String title = null;
private String description = null;
private Category parentCategory;
private CategoryType categoryType;
private Set<Category> childCategories = new HashSet<Category>();
private Date creationDate = new Date();
..................
}
public class CategoryType implements Serializable, Comparable<Object> { private int id = 0;
private String title = null;
private String description = null;
private Date creationDate = new Date();
...................
}
with the following mapping in the Category.hbm.xml to the CategoryType class:
Code:
<many-to-one name="categoryType"
column="id_type"
insert="false"
update="false"
cascade="none"
class="CategoryType"
not-null="true"
outer-join="true"
foreign-key="category_fk_id_type"/>
Then i just want to get all the categories of a particular category type:
Code:
public Collection<?> findCategoryByCategoryType(int categoryTypeId)
throws InfrastructureException {
Session session = HibernateUtil.getSession();
Collection<?> categories;
CategoryType categoryType = new CategoryType();
categoryType.setId(categoryTypeId);
try {
Criteria crit = session.createCriteria(Category.class);
categories = crit.add(Example.create(categoryType)).list();
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
return categories;
}
but im getting the following exception:
Code:
java.lang.ClassCastException: com.cms.model.category.CategoryType
at org.hibernate.criterion.Example.getEntityMode(Example.java:256)
at org.hibernate.criterion.Example.toSqlString(Example.java:186)
I don“t know what i am doing wrong, could someone please help me?
Thanks in advance