Explanation
I was receiving a class cast exception which told me to set the "hibernate.cglib.use_reflection_optimizer=false" for more specific info.
Upon doing that, I got a much more specific exception. I debugged the code to find the root cause and came up with this:
In org.hibernate.property.BasicPropertyAccessor on line 105 ,
"return method.invoke(target, null)".
The method is an instance method named "getCompanyNumber" in the Payer Class, However the target being passed in as the implementation for the invocation is the Contract Class.
I've used the debug tools to verify that if Payer class is passed instead, it will work perfectly.
I thought maybe the hashCode() needed to be implemented incase its choosing the wrong class based on the PK/hashcode but debugging the hashcodes shows they are never called.
Hibernate version:
3.0.5
Mapping documents:
Contract.hbm.xml
Code:
<hibernate-mapping default-lazy="false" package="xxx.xxx.xxx.xxx.core">
<class name="Contract" table="CHDRPF"
batch-size="50" mutable="false">
<cache usage="read-only"/>
<composite-id>
<key-property name="companyNumber" column="CHDRCOY"/>
<key-property name="contractNumber" column="CHDRNUM"/>
<key-property name="transactionNumber" column="TRANNO"/>
</composite-id>
<one-to-one name="payer" class="Payer" outer-join="false"/>
</class>
</hibernate-mapping>
Payer.hbm.xml
Code:
<hibernate-mapping default-lazy="false" package="xxx.xxx.xxx.xxx.core">
<class name="Payer" table="PAYR" batch-size="20" mutable="false">
<cache usage="read-only"/>
<composite-id>
<key-property name="companyNumber" column="CHDRCOY"/>
<key-property name="contractNumber" column="CHDRNUM"/>
<key-property name="transactionNumber" column="TRANNO"/>
</composite-id>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Full stack trace of any exception that occurs:Code:
2008-07-18 12:49:13,574 ERROR org.hibernate.property.BasicPropertyAccessor.get - IllegalArgumentException in class: cxxx.xxx.xxx.xxx.core.Payer, getter method of property: companyNumber
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of xxx.xxx.xxx.xxx.core.Payer.companyNumber
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:119)
at org.hibernate.tuple.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:61)
at org.hibernate.tuple.AbstractComponentTuplizer.getPropertyValues(AbstractComponentTuplizer.java:67)
at org.hibernate.tuple.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:50)
at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:257)
at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:158)
at org.hibernate.engine.EntityKey.getHashCode(EntityKey.java:68)
at org.hibernate.engine.EntityKey.<init>(EntityKey.java:41)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:69)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:655)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:261)
at org.hibernate.type.EntityType.resolve(EntityType.java:286)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:105)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:530)
at org.hibernate.loader.Loader.doQuery(Loader.java:436)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.doList(Loader.java:1593)
at org.hibernate.loader.Loader.list(Loader.java:1577)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:111)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1322)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
at xxx.xxx.xxx.xxx.FacadeBean.findHistoricContractTransactions(FacadeBean.java:695)
at xxx.xxx.xxx.xxx..TestFacade.testRetrieveTransactionHistory(TestFacade.java:637)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:105)
... 43 more
Thank you for all your help in advance.