Hibernate version: 2.1.6
I have a one-to-one relationship between User and Principal. If I modify a detached version of User and call session.update(user); then everything works perfectly fine. However, if I attempt to modify a detached version of user and attempt to save that user in a session that has already loaded user then I obviously get an exception. So I changed session.update(user) to session.saveOrUpdateCopy(user) and I get the ClassCastException below. The problem appears to be that when the saveOrUpdateCopy is traversing through the object graph when it gets to principal.user it thinks that the id for that relationship is the actual User class and not the User.id.
When the mapping files are loaded and the EntityType constructor is called for the Principal->User one-to-one relationship it sets EntityType.uniqueKeyPropertyName = "user" (Line 45 EntityType.class) I don't know if that's what it should do or not but that appears to be what EntityType.getIdentifier (line 72 or EntityType.class) is using to accidentally return the user as the id.
Any ideas?
Mike
User:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping
>
<class
name="com.avaloninc.account.data.User"
table="users"
lazy="true"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<cache usage="transactional" />
<id
name="id"
column="user_id"
type="java.lang.Long"
access="property"
>
<generator class="native">
<param name="sequence">user_seq</param>
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-User.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<many-to-one
name="account"
class="com.avaloninc.account.data.Account"
cascade="none"
outer-join="false"
update="true"
insert="true"
access="property"
column="account_id"
/>
<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="first_name"
/>
<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="last_name"
/>
<one-to-one
name="principal"
class="com.avaloninc.account.data.Principal"
cascade="all"
outer-join="false"
constrained="true"
access="property"
property-ref="user"
/>
<property
name="email"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="email"
not-null="true"
/>
<many-to-one
name="group"
class="com.avaloninc.account.data.Group"
cascade="none"
outer-join="false"
update="true"
insert="true"
access="property"
column="group_id"
/>
<property
name="deleted"
type="boolean"
update="true"
insert="true"
access="property"
column="deleted"
not-null="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-User.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Principal:Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping
>
<class
name="com.avaloninc.account.data.Principal"
table="principals"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<cache usage="transactional" />
<id
name="id"
column="principal_id"
type="java.lang.Long"
access="property"
>
<generator class="native">
<param name="sequence">principal_seq</param>
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Principal.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<set
name="securableFeatures"
table="account_permission_features"
lazy="true"
inverse="false"
cascade="none"
sort="unsorted"
access="property"
>
<key
column="principal_id"
>
</key>
<many-to-many
class="com.avaloninc.account.data.Feature"
column="feature_id"
outer-join="false"
/>
</set>
<set
name="approvalFeatures"
table="account_approval_features"
lazy="true"
inverse="false"
cascade="none"
sort="unsorted"
access="property"
>
<key
column="principal_id"
>
</key>
<many-to-many
class="com.avaloninc.account.data.Feature"
column="feature_id"
outer-join="false"
/>
</set>
<many-to-one
name="user"
class="com.avaloninc.account.data.User"
cascade="none"
outer-join="false"
update="true"
insert="true"
access="property"
column="user_id"
unique="true"
/>
<property
name="password"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="password"
not-null="true"
/>
<property
name="username"
type="java.lang.String"
update="false"
insert="true"
access="property"
column="username"
not-null="true"
unique="true"
/>
<property
name="defaultUser"
type="java.lang.Boolean"
update="true"
insert="true"
access="property"
column="defaultUser"
not-null="true"
/>
<property
name="disabled"
type="boolean"
update="true"
insert="true"
access="property"
column="disabled"
not-null="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Principal.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Full stack trace of any exception that occurs:Code:
java.lang.ClassCastException: com.avaloninc.account.data.User
at net.sf.hibernate.type.LongType.set(LongType.java:32)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:48)
at net.sf.hibernate.type.NullableType.nullSafeSet(NullableType.java:35)
at net.sf.hibernate.loader.Loader.bindPositionalParameters(Loader.java:749)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:788)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:265)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
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.loadByUniqueKey(EntityLoader.java:55)
at net.sf.hibernate.persister.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1110)
at net.sf.hibernate.impl.SessionImpl.loadByUniqueKey(SessionImpl.java:3848)
at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:207)
at net.sf.hibernate.type.EntityType.copy(EntityType.java:113)
at net.sf.hibernate.type.TypeFactory.copy(TypeFactory.java:284)
at net.sf.hibernate.impl.SessionImpl.doCopy(SessionImpl.java:4042)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdateCopy(SessionImpl.java:3980)
at org.springframework.orm.hibernate.HibernateTemplate$15.doInHibernate(HibernateTemplate.java:400)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:228)
at org.springframework.orm.hibernate.HibernateTemplate.saveOrUpdateCopy(HibernateTemplate.java:397)
at com.avaloninc.account.logic.UserLogic.updateUser(UserLogic.java:192)
at com.avaloninc.account.logic.UserLogic$$FastClassByCGLIB$$7f88ca46.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:676)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:121)
at org.springframework.orm.hibernate.HibernateInterceptor.invoke(HibernateInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:143)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:56)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:143)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:619)
at com.avaloninc.account.logic.UserLogic$$EnhancerByCGLIB$$fc3968fc.updateUser(<generated>)
at com.avaloninc.account.logic.impl.UserLogicTest.testUpdateUser(UserLogicTest.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
[/code]