Hibernate version:
2.1.7c
Mapping documents:
class Portal {
String name;
Collection crefs;
}
abstract class CRef {
String name;
Portal portal;
}
class ComponentCRef extends CRef;
class FolderCRef extends CRef;
<class name="com.hk.tools.model.portal.Portal" table="HKPORTALDEFN">
<id name="name"
type="string"
column="PORTAL_NAME"
length="15"
unsaved-value="null">
<generator class="assigned"/>
</id>
<set name="crefs"
inverse="true">
<key column="PORTAL_NAME"/>
<one-to-many class="com.hk.tools.model.portal.CRef"/>
</set>
</class>
<class
name="com.hk.tools.model.portal.CRef"
table="HKPORTALCREF">
<composite-id>
<key-property name="name" column="CREF_NAME" length="15"/>
<key-many-to-one name="portal" column="PORTAL_NAME" />
</composite-id>
<discriminator column="CREF_TYPE" type="string" length="6"/>
<subclass name="com.hk.tools.model.portal.ComponentCRef"
discriminator-value="COMP">
</subclass>
<subclass name="com.hk.tools.model.portal.FolderCRef"
discriminator-value="FOLDER">
</subclass>
</class>
Code between sessionFactory.openSession() and session.close():
Portal p1 = (Portal)session.load(Portal.class, "P1");
Full stack trace of any exception that occurs:
Exception in thread "main" net.sf.hibernate.InstantiationException:
Could not instantiate component with CGLIB: com.hk.tools.model.portal.CRef
at
net.sf.hibernate.type.ComponentType.instantiate(ComponentType.java:306)
at
net.sf.hibernate.type.ComponentType.instantiate(ComponentType.java:320)
at
net.sf.hibernate.type.ComponentType.resolveIdentifier(ComponentType.java:403)
at
net.sf.hibernate.type.ComponentType.nullSafeGet(ComponentType.java:145)
at net.sf.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:427)
at net.sf.hibernate.loader.Loader.getRowFromResultSet(Loader.java:200)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:281)
at
net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:990)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:965)
at
net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:93)
at
net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:288)
at
net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3303)
at
net.sf.hibernate.collection.PersistentCollection.forceInitialization(PersistentCollection.java:336)
at
net.sf.hibernate.impl.SessionImpl.initializeNonLazyCollections(SessionImpl.java:3156)
at
net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:911)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:931)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
at
net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:415)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2130)
at
net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:2000)
at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1929)
at test.Test.main(Test.java:61)
Caused by: java.lang.InstantiationError: com.hk.tools.model.portal.CRef
at
com.hk.tools.model.portal.CRef$$FastClassByCGLIB$$6e1f099c.newInstance(<generated>)
at net.sf.cglib.reflect.FastClass.newInstance(FastClass.java:94)
at
net.sf.hibernate.type.ComponentType.instantiate(ComponentType.java:303)
... 24 more
Name and version of the database you are using:
hsqldb
The generated SQL (show_sql=true):
Hibernate: select portal0_.PORTAL_NAME as PORTAL_N1_0_,
portal0_.PORTAL_DESC as PORTAL_D2_0_, portal0_.ACTIVE as ACTIVE0_,
portal0_.ROW_ADDED_DATE as ROW_ADDE4_0_, portal0_.ROW_ADDED_USER as
ROW_ADDE5_0_, portal0_.ROW_MODIFIED_DATE as ROW_MODI6_0_,
portal0_.ROW_MODIFIED_USER as ROW_MODI7_0_ from HKPORTALDEFN portal0_
where portal0_.PORTAL_NAME=?
Debug level Hibernate log excerpt:
00:38:59,854 DEBUG BatcherImpl:252 - preparing statement
00:38:59,994 DEBUG StringType:46 - binding 'P1' to parameter: 1
00:39:00,024 DEBUG Loader:277 - processing result set
00:39:00,034 DEBUG Loader:480 - result row: P1
00:39:00,044 DEBUG Loader:611 - Initializing object from ResultSet: P1
00:39:00,044 DEBUG Loader:680 - Hydrating entity:
com.hk.tools.model.portal.Portal#P1
00:39:00,054 DEBUG StringType:68 - returning 'Desc P1' as column:
PORTAL_D2_0_
00:39:00,064 DEBUG YesNoType:68 - returning 'true' as column: ACTIVE0_
00:39:00,074 DEBUG TimestampType:64 - returning null as column: ROW_ADDE4_0_
00:39:00,084 DEBUG StringType:64 - returning null as column: ROW_ADDE5_0_
00:39:00,124 DEBUG TimestampType:64 - returning null as column: ROW_MODI6_0_
00:39:00,124 DEBUG StringType:64 - returning null as column: ROW_MODI7_0_
00:39:00,134 DEBUG Loader:294 - done processing result set (1 rows)
00:39:00,134 DEBUG BatcherImpl:210 - done closing: 0 open
PreparedStatements, 0 open ResultSets
00:39:00,134 DEBUG BatcherImpl:272 - closing statement
00:39:00,134 DEBUG Loader:314 - total objects hydrated: 1
00:39:00,144 DEBUG SessionImpl:2215 - resolving associations for
[com.hk.tools.model.portal.Portal#P1]
00:39:00,154 DEBUG SessionImpl:3982 - creating collection
wrapper:[com.hk.tools.model.portal.Portal.crefs#P1]
00:39:00,174 DEBUG SessionImpl:2246 - done materializing entity
[com.hk.tools.model.portal.Portal#P1]
00:39:00,174 DEBUG SessionImpl:3149 - initializing non-lazy collections
00:39:00,174 DEBUG SessionImpl:3295 - initializing collection
[com.hk.tools.model.portal.Portal.crefs#P1]
00:39:00,174 DEBUG SessionImpl:3296 - checking second-level cache
00:39:00,184 DEBUG SessionImpl:3302 - collection not cached
00:39:00,184 DEBUG BatcherImpl:203 - about to open: 0 open
PreparedStatements, 0 open ResultSets
Hibernate: select crefs0_.PORTAL_NAME as PORTAL_N2___, crefs0_.CREF_NAME
as CREF_NAME__, crefs0_.CREF_NAME as CREF_NAME0_, crefs0_.PORTAL_NAME as
PORTAL_N2_0_, crefs0_.CREF_TYPE as CREF_TYPE0_, crefs0_.CREF_LABEL as
CREF_LABEL0_, crefs0_.CREF_DESC as CREF_DESC0_, crefs0_.CREF_ORDER as
CREF_ORDER0_ from HKPORTALCREF crefs0_ where crefs0_.PORTAL_NAME=?
00:39:00,184 DEBUG BatcherImpl:252 - preparing statement
00:39:00,184 DEBUG StringType:46 - binding 'P1' to parameter: 1
00:39:00,194 DEBUG Loader:402 - result set contains (possibly empty)
collection: [com.hk.tools.model.portal.Portal.crefs#P1]
00:39:00,204 DEBUG SessionImpl:3038 - uninitialized collection: initializing
00:39:00,204 DEBUG Loader:277 - processing result set
00:39:00,204 DEBUG StringType:68 - returning 'cref11' as column: CREF_NAME0_
00:39:00,214 DEBUG StringType:68 - returning 'P1' as column: PORTAL_N2_0_
00:39:00,224 DEBUG BatcherImpl:210 - done closing: 0 open
PreparedStatements, 0 open ResultSets
00:39:00,224 DEBUG BatcherImpl:272 - closing statement
Exception in thread "main" net.sf.hibernate.InstantiationException:
It can recognize the subclass correctly because if I delete abstract keyword then I got exception
00:17:16,950 DEBUG BatcherImpl:252 - preparing statement
00:17:17,140 DEBUG StringType:46 - binding 'P1' to parameter: 1
00:17:17,170 DEBUG Loader:277 - processing result set
00:17:17,180 DEBUG Loader:480 - result row: P1
00:17:17,191 DEBUG Loader:611 - Initializing object from ResultSet: P1
00:17:17,201 DEBUG Loader:680 - Hydrating entity:
com.hk.tools.model.portal.Portal#P1
00:17:17,221 DEBUG StringType:68 - returning 'Desc P1' as column:
PORTAL_D2_0_
00:17:17,221 DEBUG YesNoType:68 - returning 'true' as column: ACTIVE0_
00:17:17,231 DEBUG TimestampType:64 - returning null as column: ROW_ADDE4_0_
00:17:17,241 DEBUG StringType:64 - returning null as column: ROW_ADDE5_0_
00:17:17,281 DEBUG TimestampType:64 - returning null as column: ROW_MODI6_0_
00:17:17,281 DEBUG StringType:64 - returning null as column: ROW_MODI7_0_
00:17:17,291 DEBUG Loader:294 - done processing result set (1 rows)
00:17:17,301 DEBUG BatcherImpl:210 - done closing: 0 open
PreparedStatements, 0 open ResultSets
00:17:17,301 DEBUG BatcherImpl:272 - closing statement
00:17:17,301 DEBUG Loader:314 - total objects hydrated: 1
00:17:17,311 DEBUG SessionImpl:2215 - resolving associations for
[com.hk.tools.model.portal.Portal#P1]
00:17:17,331 DEBUG SessionImpl:3982 - creating collection
wrapper:[com.hk.tools.model.portal.Portal.crefs#P1]
00:17:17,411 DEBUG SessionImpl:2246 - done materializing entity
[com.hk.tools.model.portal.Portal#P1]
00:17:17,411 DEBUG SessionImpl:3149 - initializing non-lazy collections
00:17:17,421 DEBUG SessionImpl:3295 - initializing collection
[com.hk.tools.model.portal.Portal.crefs#P1]
00:17:17,421 DEBUG SessionImpl:3296 - checking second-level cache
00:17:17,421 DEBUG SessionImpl:3302 - collection not cached
00:17:17,421 DEBUG BatcherImpl:203 - about to open: 0 open
PreparedStatements, 0 open ResultSets
Hibernate: select crefs0_.PORTAL_NAME as PORTAL_N2___, crefs0_.CREF_NAME
as CREF_NAME__, crefs0_.CREF_NAME as CREF_NAME0_, crefs0_.PORTAL_NAME as
PORTAL_N2_0_, crefs0_.CREF_TYPE as CREF_TYPE0_, crefs0_.CREF_LABEL as
CREF_LABEL0_, crefs0_.CREF_DESC as CREF_DESC0_, crefs0_.CREF_ORDER as
CREF_ORDER0_ from HKPORTALCREF crefs0_ where crefs0_.PORTAL_NAME=?
00:17:17,431 DEBUG BatcherImpl:252 - preparing statement
00:17:17,431 DEBUG StringType:46 - binding 'P1' to parameter: 1
00:17:17,441 DEBUG Loader:402 - result set contains (possibly empty)
collection: [com.hk.tools.model.portal.Portal.crefs#P1]
00:17:17,451 DEBUG SessionImpl:3038 - uninitialized collection: initializing
00:17:17,471 DEBUG Loader:277 - processing result set
00:17:17,471 DEBUG StringType:68 - returning 'cref11' as column: CREF_NAME0_
00:17:17,471 DEBUG StringType:68 - returning 'P1' as column: PORTAL_N2_0_
xxx: constructor
00:17:17,481 DEBUG SessionImpl:1995 - loading
[com.hk.tools.model.portal.Portal#P1]
00:17:17,481 DEBUG SessionImpl:2093 - attempting to resolve
[com.hk.tools.model.portal.Portal#P1]
00:17:17,481 DEBUG SessionImpl:2109 - resolved object in session cache
[com.hk.tools.model.portal.Portal#P1]
xxx hash:
00:17:17,491 DEBUG Loader:480 - result row:
com.hk.tools.model.portal.CRef@af65ab70
xxx hash:
00:17:17,491 DEBUG StringType:68 - returning 'COMP' as column: CREF_TYPE0_
00:17:17,551 DEBUG BatcherImpl:210 - done closing: 0 open
PreparedStatements, 0 open ResultSets
00:17:17,551 DEBUG BatcherImpl:272 - closing statement
Exception in thread "main" net.sf.hibernate.WrongClassException: Object
with id: com.hk.tools.model.portal.CRef@af65ab70 was not of the
specified subclass: com.hk.tools.model.portal.CRef (Discriminator: COMP)
|