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.