-->
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.  [ 5 posts ] 
Author Message
 Post subject: Inheritance mapping
PostPosted: Tue Jun 03, 2008 4:40 am 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 03, 2008 1:47 pm 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Hi, anyone knows whats the problem? im really stuck at this.

Thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 10:11 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
My guess is that the generics syntax is asking for one type of object, and getting another. Any chance?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 04, 2008 11:29 am 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
thanks for answering, these are the calls to my findAll() method:

Code:
List<? super Category> resourceCategoryList = (List<? super Category>) categoryDao.findAll(ResourceCategory.class, false);
      request.setAttribute("resourceCategoryList", resourceCategoryList);
      // UserCategory
      List<? super Category> userCategoryList = (List<? super Category>) categoryDao.findAll(UserCategory.class, false);
      request.setAttribute("userCategoryList", userCategoryList);
      // ProfileCategory
      List<? super Category> profileCategoryList = (List<? super Category>) categoryDao.findAll(ProfileCategory.class, false);
      request.setAttribute("profileCategoryList", profileCategoryList);


something weird is happening, if i skip the ProfileCategory call, it works ok, getting all my resource and user categories...

I really dont know why...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 3:47 pm 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
hi again,

I am still trying to solve this problem, butno luck still. This is the queries hibernate is executing:

Code:
Hibernate: select this_.id as id1_0_, this_.title as title1_0_, this_.description as descript3_1_0_, this_.id_parent as id4_1_0_, this_.creation_date as creation5_1_0_ from resource_category this_
Hibernate: select this_.id as id1_0_, this_.title as title1_0_, this_.description as descript3_1_0_, this_.id_parent as id4_1_0_, this_.creation_date as creation5_1_0_ from user_category this_


Does anyone knows where is the problem?

Thanks in advance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.