-->
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.  [ 2 posts ] 
Author Message
 Post subject: org.hibernate.WrongClassException with Table Per Class inher
PostPosted: Wed Apr 04, 2007 2:42 am 
Beginner
Beginner

Joined: Thu Nov 16, 2006 11:34 am
Posts: 26
Location: Boston
I'm using Spring 2.0, Hibernate 3.2 & annotations for pojos
I have a scenario with Table Per Class inheritance.I also OpenSessionInViewFilter.

Code:
@Entity
@Table (name = "eligibility_criterias")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@GenericGenerator(name="id-generator", strategy = "native",
     parameters = {
         @Parameter(name="sequence", value="ELIGIBILITY_CRITERIAS_ID_SEQ")
     }
)
public abstract class EligibilityCriteria
{         
   ... //some attributes //
   
   private Study study;
   
   @ManyToOne (fetch = FetchType.LAZY)
   @JoinColumn(name="stu_id", updatable = false, insertable = false) 
   public Study getStudy() {
      return study;
   }
      .....
      .....
}


Code:
@Entity
@DiscriminatorValue(value="I")
public class InclusionEligibilityCriteria extends EligibilityCriteria
{}



Code:
@Entity
@DiscriminatorValue(value="E")
public class ExclusionEligibilityCriteria extends EligibilityCriteria
{}


Code:
public class Study {
.... //some attributes//

private List<InclusionEligibilityCriteria> incCriterias= new ArrayList<InclusionEligibilityCriteria>();
   private List<ExclusionEligibilityCriteria> excCriterias= new ArrayList<ExclusionEligibilityCriteria>();

...
@OneToMany
@Cascade (value = { CascadeType.ALL, CascadeType.DELETE_ORPHAN })       
@JoinColumn(name = "stu_id", nullable=false)       
CascadeType.DELETE_ORPHAN})   
public List<InclusionEligibilityCriteria> getIncCriterias() {
   return incCriterias;
}

@OneToMany
@Cascade (value = { CascadeType.ALL, CascadeType.DELETE_ORPHAN })       
@JoinColumn(name = "stu_id", nullable=false)
   
public List<ExclusionEligibilityCriteria> getExcCriterias() {
     return excCriterias;
}


With the above setup when I create a Study, it gets created along with discriminators in the default column 'DTYPE'. However when I load a Study object with these associations, study.getStudyDesignById(id) - I get the following error;
Code:

2007-04-04 02:20:27,640 DEBUG [org.hibernate.SQL] - select inccriteri0_.stu_id as stu8_1_, inccriteri0_.id as id1_, inccriteri0_.id as id15_0_, inccriteri0_.version as version15_0_, inccriteri0_.grid_id as grid4_15_0_, inccriteri0_.stu_id as stu8_15_0_, inccriteri0_.not_applicable_indicator as not5_15_0_, inccriteri0_.question_number as question6_15_0_, inccriteri0_.question_text as question7_15_0_ from eligibility_criterias inccriteri0_ where inccriteri0_.stu_id=?
Hibernate: select inccriteri0_.stu_id as stu8_1_, inccriteri0_.id as id1_, inccriteri0_.id as id15_0_, inccriteri0_.version as version15_0_, inccriteri0_.grid_id as grid4_15_0_, inccriteri0_.stu_id as stu8_15_0_, inccriteri0_.not_applicable_indicator as not5_15_0_, inccriteri0_.question_number as question6_15_0_, inccriteri0_.question_text as question7_15_0_ from eligibility_criterias inccriteri0_ where inccriteri0_.stu_id=?
2007-04-04 02:20:27,640 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open ResultSet (open ResultSets: 0, globally: 0)
2007-04-04 02:20:27,640 DEBUG [org.hibernate.loader.Loader] - result set contains (possibly empty) collection: [xxx.domain.Study.incCriterias#1]
2007-04-04 02:20:27,640 DEBUG [org.hibernate.loader.Loader] - result row: EntityKey[xxx.domain.InclusionEligibilityCriteria#3]
2007-04-04 02:20:27,640 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close ResultSet (open ResultSets: 1, globally: 1)
2007-04-04 02:20:27,640 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2007-04-04 02:20:27,656 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@3a4822
2007-04-04 02:20:27,656 DEBUG [org.springframework.web.servlet.DispatcherServlet] - Could not complete request
org.hibernate.WrongClassException: Object with id: 3 was not of the specified subclass: xxx.domain.InclusionEligibilityCriteria (loaded object was of wrong class class xxx.domain.ExclusionEligibilityCriteria)
   at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1234)
   at org.hibernate.loader.Loader.getRow(Loader.java:1186)
   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:568)
   at org.hibernate.loader.Loader.doQuery(Loader.java:689)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
   at org.hibernate.loader.Loader.loadCollection(Loader.java:1985)
   at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
   at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
   at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
   at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
   at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
   at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:109)
   at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
   at xxx.dao.StudyDao.getStudyDesignById(StudyDao.java:55)


Code:
/**
    * This is a hack to load all collection objects in memory. Useful
    * for editing a Study when you know you will be needing all collections
    * To avoid Lazy loading Exception by Hibernate, a call to .size() is done
    * for each collection
    * @param id
    * @return
    */
   public Study getStudyDesignById(int id) {            
        Study study =  (Study) getHibernateTemplate().get(domainClass(), id);
       
     
        study.getExcCriterias().size();
        study.getIncCriterias().size();
       
       //..do the same for other collections   
       
        return study;
    }


I tried to search a lot but barely any reasonable responses.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 04, 2007 11:03 am 
Beginner
Beginner

Joined: Thu Nov 16, 2006 11:34 am
Posts: 26
Location: Boston
Finally got this working after going through a couple of hibernate forums. The solution is, I add a where clause for the discriminator while loading the collections

Study

Code:
@OneToMany
    @Cascade (value = { CascadeType.ALL, CascadeType.DELETE_ORPHAN })       
    @JoinColumn(name = "stu_id", nullable=false)
    @Where(clause = "DTYPE = 'E'") // This is pretty lame //that this is necessary   :(
   public List<ExclusionEligibilityCriteria> getExcCriterias() {
      return excCriterias;
   }


Code:
@OneToMany
    @Cascade (value = { CascadeType.ALL, CascadeType.DELETE_ORPHAN })       
    @JoinColumn(name = "stu_id", nullable=false)   
    @Where(clause = "DTYPE = 'I'") // it is pretty lame that this is necessary         
   public List<InclusionEligibilityCriteria> getIncCriterias() {
      return incCriterias;
   }


Pretty lame! But looks like this is the only way to tell hibernate to distinguish while loading an inherited class.


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