-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: ClassCastException updating object with saveOrUpdateCopy
PostPosted: Tue Nov 16, 2004 8:27 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
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]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 17, 2004 12:52 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
Anyone have any ideas? Is this a bug? Or am I doing something dumb? Did I not provide enough information or did I provide too much? :)

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 18, 2004 2:01 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
take a look at the id-generator of the Principal! ...

The generator should be "foreign" ... take a look e.g. at HiA Pages 220-224 ...

And i think the "constrained=true" on User-side is wrong, too.

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 18, 2004 3:35 pm 
Regular
Regular

Joined: Thu Aug 26, 2004 9:23 pm
Posts: 71
Thanks for your help curio. I removed contrained=true and it did not help. But when I changed the relationship from a foreign key association to a primary key association it did fix the problem. Though I am still curious why the foreign key association I was using before (found on pages 221-223 of HiA) didn't work with hibernate.saveOrUpdateCopy but it did work with hibernate.update.

It's a little disconcerting but I should survice since the primary key association seems to work. So maybe I'll submit a bug about it anyway.

Anyone object?

Thanks,
Mike


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.