Hello everyone,
I am trying to map an inheritance relationship, but i am getting an error.
This is my mapping file:
Code:
<hibernate-mapping package="com.model.category">
<class name="Category"
lazy="true">
<id name="id"
type="integer"
column="id"
unsaved-value="null">
<generator class="native"/>
</id>
<property name="title"
type="string"
column="title"
length="40"
not-null="true"
unique-key="UNIQUE_NAME_AT_LEVEL"/>
<property name="description"
type="string"
column="description"
length="200"
not-null="false"/>
<!-- Parent can be null for root categories. -->
<many-to-one name="parentCategory"
cascade="none"
outer-join="false"
foreign-key="gatalents_user_category_fk_id_parent">
<column name="id_parent"
not-null="false"
unique-key="UNIQUE_NAME_AT_LEVEL"/>
</many-to-one>
<!-- We can't change the creation time, so map it with update="false". -->
<property name="creationDate"
column="creation_date"
type="java.util.Date"
update="false"
not-null="true"/>
<union-subclass name="UserCategory" table="user_category" />
<union-subclass name="ProfileCategory" table="profile_category" />
<union-subclass name="ItemCategory" table="item_category" />
<union-subclass name="ResourceCategory" table="resource_category" />
</class>
</hibernate-mapping>
This is the method where the exception is thrown:
Code:
public Collection<?> findAll(Class clazz, boolean onlyRootCategories)
throws InfrastructureException {
Collection<?> categories;
try {
if (onlyRootCategories) {
Criteria crit = HibernateUtil.getSession().createCriteria(clazz);
categories = crit.add(Expression.isNull("parentCategory")).list();
} else {
categories = HibernateUtil.getSession().createCriteria(clazz).list();
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
return categories;
}
and the exception:
Code:
com.exception.InfrastructureException: org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: com.model.category.ProfileCategory (loaded object was of wrong class class com.model.category.UserCategory)
at com.dao.CategoryDAO.findAll(CategoryDAO.java:64)
at com.controller.servlet.UserForm.service(UserForm.java:68)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: com.model.category.ProfileCategory (loaded object was of wrong class class com.model.category.UserCategory)
at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1243)
at org.hibernate.loader.Loader.getRow(Loader.java:1195)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at com.dao.CategoryDAO.findAll(CategoryDAO.java:61)
... 14 more
Can someone help me? i am stuck at this.
Thanks in advance