java.lang.IllegalArgumentException: object is not an instance of declaring class

I’ve found a lot of StackOverflow questions on this topic that use Annotations, and not been able to map that to the HBM files we are using, and a lot of answers for HBM users saying “I fixed it now, it was a simple mapping error” but not saying what was wrong or how they fixed it. A search for “object is not an instance of declaring class” on these forums give no results, so here goes.

We want to fetch all MaterialCollections that belong to a given UserGroup. We’re not interested in loading the user group itself at this point, but Hibernate identifies that USER_GROUP_ID belongs to UserGroup. Small problem: The query does not have a UserGroup reference, only one for MaterialCollection.

Hibernate 5.6.10.Final, Hibernate Mapping DTD 3.0

Java classes:

class UserGroup {
  long id;
  // snip
}

class MaterialCollection {
  long id;
  MaterialCollection parentMaterialCollection;
  String name;
  UserGroup userGroup;
  // snip
}

Material.hbm.xml:

<hibernate-mapping>
    <class name="se.zaleth.jar.material.MaterialCollection" table="R019_MATERIAL_COLL">
        <id column="COLL_ID" name="id">
            <generator class="sequence">
                <param name="sequence">R019_SEQ</param>
                <param name="increment_size">1</param>
                <param name="parameters">INCREMENT BY 1 START WITH 50001</param>
            </generator>
        </id>
        <many-to-one class="se.zaleth.jar.material.MaterialCollection" column="PARENT_COLL_ID" name="parentMaterialCollection"/>
        <property name="name" not-null="true" type="se.zaleth.util.UTFStringType">
            <column name="COLL_NAME" sql-type="nvarchar(1000)"/>
        </property>
        <many-to-one class="se.zaleth.jar.administration.UserGroup" column="USERGROUP_ID" name="userGroup" cascade="all" lazy="proxy"/>
    </class>
</hibernate-mapping>

Exception stack trace:

javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of se.zaleth.jar.administration.UserGroup.id
	at org.hibernate.bugs.JPAUnitTestCase.hhh123Test(JPAUnitTestCase.java:172)
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of se.zaleth.jar.administration.UserGroup.id
	at org.hibernate.bugs.JPAUnitTestCase.hhh123Test(JPAUnitTestCase.java:172)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
	at org.hibernate.bugs.JPAUnitTestCase.hhh123Test(JPAUnitTestCase.java:172)

Logging trace:

16:03:01.974 [main] TRACE org.hibernate.engine.query.spi.HQLQueryPlan - Find: from MaterialCollection as coll where coll.userGroup = :USER_GROUP_ID and coll.parentMaterialCollection is null order by coll.name 
16:03:01.990 [main] TRACE org.hibernate.engine.spi.QueryParameters - Named parameters: {USER_GROUP_ID=se.zaleth.jar.administration.UserGroup#51101}
16:03:01.990 [main] DEBUG org.hibernate.SQL - 
    select
        materialco0_.COLL_ID as coll_id1_0_,
        materialco0_.PARENT_COLL_ID as parent_coll_id2_0_,
        materialco0_.COLL_NAME as coll_name3_0_,
        materialco0_.USERGROUP_ID as usergroup_id4_0_ 
    from
        R019_MATERIAL_COLL materialco0_ 
    where
        materialco0_.USERGROUP_ID=? 
        and (
            materialco0_.PARENT_COLL_ID is null
        ) 
    order by
        materialco0_.COLL_NAME
Hibernate: 
    select
        materialco0_.COLL_ID as coll_id1_0_,
        materialco0_.PARENT_COLL_ID as parent_coll_id2_0_,
        materialco0_.COLL_NAME as coll_name3_0_,
        materialco0_.USERGROUP_ID as usergroup_id4_0_ 
    from
        R019_MATERIAL_COLL materialco0_ 
    where
        materialco0_.USERGROUP_ID=? 
        and (
            materialco0_.PARENT_COLL_ID is null
        ) 
    order by
        materialco0_.COLL_NAME
16:03:02.005 [main] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Registering statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@dd737ea]
16:03:02.021 [main] TRACE org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl - Registering last query statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@dd737ea]
16:03:02.021 [main] ERROR org.hibernate.property.access.spi.GetterMethodImpl - HHH000122: IllegalArgumentException in class: se.zaleth.jar.administration.UserGroup, getter method of property: id
16:03:02.021 [main] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Releasing statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@dd737ea]
16:03:02.021 [main] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Closing prepared statement [oracle.jdbc.driver.OraclePreparedStatementWrapper@dd737ea]
16:03:02.021 [main] TRACE org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl - Starting after statement execution processing [AFTER_TRANSACTION]
16:03:02.021 [main] DEBUG org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl - JDBC transaction marked for rollback-only (exception provided for stack trace)
java.lang.Exception: exception just for purpose of providing stack trace
	at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.markRollbackOnly(JdbcResourceLocalTransactionCoordinatorImpl.java:324) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
	at org.hibernate.engine.transaction.internal.TransactionImpl.markRollbackOnly(TransactionImpl.java:203) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
	at org.hibernate.internal.AbstractSharedSessionContract.markForRollbackOnly(AbstractSharedSessionContract.java:428) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
	at org.hibernate.internal.ExceptionConverterImpl.handlePersistenceException(ExceptionConverterImpl.java:297) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1626) ~[hibernate-core-5.6.10.Final.jar:5.6.10.Final]
	at org.hibernate.bugs.JPAUnitTestCase.hhh123Test(JPAUnitTestCase.java:172) ~[test-classes/:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) ~[junit-4.12.jar:4.12]
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) ~[junit-4.12.jar:4.12]
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) ~[junit-4.12.jar:4.12]
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) ~[junit-4.12.jar:4.12]
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) ~[junit-4.12.jar:4.12]
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) ~[junit-4.12.jar:4.12]
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) ~[junit-4.12.jar:4.12]
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) ~[junit-4.12.jar:4.12]
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) ~[junit-4.12.jar:4.12]
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) ~[junit-4.12.jar:4.12]
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) ~[junit-4.12.jar:4.12]
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) ~[junit-4.12.jar:4.12]
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) ~[junit-4.12.jar:4.12]
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363) ~[junit-4.12.jar:4.12]
	at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365) ~[surefire-junit4-2.22.0.jar:2.22.0]
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273) ~[surefire-junit4-2.22.0.jar:2.22.0]
	at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238) ~[surefire-junit4-2.22.0.jar:2.22.0]
	at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159) ~[surefire-junit4-2.22.0.jar:2.22.0]
	at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:383) ~[surefire-booter-2.22.0.jar:2.22.0]
	at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:344) ~[surefire-booter-2.22.0.jar:2.22.0]
	at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125) ~[surefire-booter-2.22.0.jar:2.22.0]
	at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:417) ~[surefire-booter-2.22.0.jar:2.22.0]
16:03:02.037 [main] DEBUG org.hibernate.engine.transaction.internal.TransactionImpl - rolling back
16:03:02.037 [main] TRACE org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor - Preparing to rollback transaction via JDBC Connection.rollback()
16:03:02.037 [main] TRACE org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor - Transaction rolled-back via JDBC Connection.rollback()
16:03:02.037 [main] TRACE org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor - LogicalConnection#afterTransaction
16:03:02.037 [main] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Releasing JDBC resources
16:03:02.037 [main] DEBUG org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl - Initiating JDBC connection release from afterTransaction
16:03:02.037 [main] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Releasing JDBC resources
16:03:02.037 [main] TRACE org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl - ResourceLocalTransactionCoordinatorImpl#afterCompletionCallback(false)
16:03:02.037 [main] TRACE org.hibernate.resource.transaction.internal.SynchronizationRegistryStandardImpl - SynchronizationRegistryStandardImpl.notifySynchronizationsAfterTransactionCompletion(5)
16:03:02.037 [main] TRACE org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor - LogicalConnection#afterTransaction
16:03:02.037 [main] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Releasing JDBC resources
16:03:02.037 [main] DEBUG org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl - Initiating JDBC connection release from afterTransaction
16:03:02.037 [main] TRACE org.hibernate.internal.SessionImpl - SessionImpl#afterTransactionCompletion(successful=false, delayed=false)

Hi @zaleth81 ,

Can you please provide the query you are trying to execute or even better point to a git repo with the entire test case?

Thanks

Here’s the test case: zaleth/HibernateBugTestCase (github.com)

I didn’t want to make a JIRA for this because it is most likely a bad configuration on our part and not a bug in Hibernate.

You are trying to compare a UserGroup with a parameter that has the type of the identifier long. You should either change your query:

from MaterialCollection as coll 
where coll.userGroup.id = :USER_GROUP_ID
and coll.parentMaterialCollection is null

or the parameter:

q.setParameter("USER_GROUP_ID", session.getReference(UserGroup.class, userGroupId));
1 Like

Just want to confirm that it does indeed work now, I spent a few hours not seeing that the exception referred to other method calls in our unit tests.

I guess the ancient version we use in prod (Hibernate 4.3.7) had some sort of autoboxing in the HQL compiler that silently resolved it every time.

Thanks a lot for the help!

1 Like