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: EnhancerByCGLIB ClassCastException with subclasses
PostPosted: Wed Apr 13, 2005 9:58 am 
Beginner
Beginner

Joined: Tue Oct 28, 2003 12:09 pm
Posts: 46
I have a case where I have the following class structure:

CourseRole extends UserRole
Post contains a UserRole called 'creator'

I do:

Code:
p.setCreator(my new Course Role)
close hibernate
load p
system.out.println(p.getCReator())
cast p to CourseRole from UserRole.
get a class cast exception.


I know this isn't proper OO, and I won't be doing it outside of the test case, but I don't understand why it's happening and I'm afraid I have something wrong in my mappings.

The confusing thing, the System.out.println(p.getCreator()) shows CourseRole, the hibernate debug log shows it loaded a CourseRole, but it's showing the superclass's proxy class in the exception (even though the subclass has lazy=true which should specifiy it as the proxy class?) ie: UserRole$$EnhancerByCGLIB$

Can you not do straight casts when using proxied hibernate objects?

I've removed a bunch of extra properties from my mappings to try to keep things simple, so they look kinda bare compared to what's happening in the debug log.

Thoughts?

-mp

Hibernate version:
2.1.8

Mapping documents:
Code:
   <class name="edu.academyart.model.Post"  dynamic-update="true" lazy="true" table="post">
      <id name="id" type="long" column="id" >
         <generator class="native"/>
      </id>
      <many-to-one name="creator" column="courseRoleId" class="edu.academyart.model.UserRole" cascade="none"/>
   </class>

Code:
<class name="edu.academyart.model.UserRole"  dynamic-update="true" lazy="true" table="userRole"
        discriminator-value="userRole">
      <id name="id" type="long" column="id">
         <generator class="native"/>
      </id>

        <discriminator column="roleType" type="string"/>

   </class>


Code:
<subclass name="edu.academyart.model.CourseRole"  discriminator-value="courseRole"
        lazy="true" extends="edu.academyart.model.UserRole">


   </subclass>


Code between sessionFactory.openSession() and session.close():

Code:
        CourseRole role = TestCourseRole.createCourseRole(hibernate, c);
        Post p = createPost(hibernate, t, role);
        assertNotNull("creation of post with CourseRole", p);

        hibernate.close();
        hibernate = getHibernateSession();
        p = (Post) hibernate.load(Post.class, p.getId());
        assertNotNull("restoring p", p);
        System.out.println(p.getCreator());
        TestCourseRole.deleteCourseRole(hibernate, (CourseRole) p.getCreator());

Full stack trace of any exception that occurs:
Code:
java.lang.ClassCastException: edu.academyart.model.UserRole$$EnhancerByCGLIB$$700f1695
   at edu.academyart.model.graduate.TestPost.testPostWithCourseRole(TestPost.java:59)
   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.junit2.JUnitStarter.main(JUnitStarter.java:31)
   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:78)

Name and version of the database you are using:
sqlserver :P

Debug level Hibernate log excerpt:

13 Apr 2005 09:47:36 DEBUG SessionImpl:2216 - resolving associations for [edu.academyart.model.Post#10572]
13 Apr 2005 09:47:36 DEBUG SessionImpl:1996 - loading [edu.academyart.model.UserRole#15164]
13 Apr 2005 09:47:36 DEBUG SessionImpl:3994 - creating collection wrapper:[edu.academyart.model.Post.attachments#10572]
13 Apr 2005 09:47:36 DEBUG SessionImpl:3994 - creating collection wrapper:[edu.academyart.model.Post.courseRolesWhoHaveViewed#10572]
13 Apr 2005 09:47:36 DEBUG SessionImpl:1996 - loading [edu.academyart.model.Topic#5074]
13 Apr 2005 09:47:36 DEBUG SessionImpl:2247 - done materializing entity [edu.academyart.model.Post#10572]
13 Apr 2005 09:47:36 DEBUG SessionImpl:3161 - initializing non-lazy collections
13 Apr 2005 09:47:36 DEBUG SessionImpl:2094 - attempting to resolve [edu.academyart.model.UserRole#15164]
13 Apr 2005 09:47:36 DEBUG SessionImpl:2130 - object not resolved in any cache [edu.academyart.model.UserRole#15164]
13 Apr 2005 09:47:36 DEBUG EntityPersister:410 - Materializing entity: [edu.academyart.model.UserRole#15164]
13 Apr 2005 09:47:36 DEBUG BatcherImpl:204 - about to open: 0 open PreparedStatements, 0 open ResultSets
13 Apr 2005 09:47:36 DEBUG SQL:230 - select userrole0_.id as id1_, userrole0_.roleType as roleType1_, userrole0_.userId as userId1_, userrole0_.roledObjectType as roledObj4_1_, userrole0_.roledObjectId as roledObj5_1_, userrole0_.fromTopclass as fromTopc6_1_, userrole0_.accessPrincipalId as accessPr7_1_, userrole0_.student as student1_, userrole0_.teacher as teacher1_, userrole0_.admin as admin1_, userrole0_.canPost as canPost1_, userrole0_.canView as canView1_, userrole0_.enrolled as enrolled1_, accessprin1_.id as id0_, accessprin1_.name as name0_ from userRole userrole0_ left outer join accessPrincipal accessprin1_ on userrole0_.accessPrincipalId=accessprin1_.id where userrole0_.id=?
13 Apr 2005 09:47:36 DEBUG BatcherImpl:253 - preparing statement
13 Apr 2005 09:47:36 DEBUG Loader:281 - processing result set
13 Apr 2005 09:47:36 DEBUG Loader:484 - result row: 3, 15164
13 Apr 2005 09:47:36 DEBUG Loader:615 - Initializing object from ResultSet: 3
13 Apr 2005 09:47:36 DEBUG Loader:684 - Hydrating entity: edu.academyart.model.AccessPrincipal#3
13 Apr 2005 09:47:36 DEBUG Loader:615 - Initializing object from ResultSet: 15164
13 Apr 2005 09:47:36 DEBUG Loader:684 - Hydrating entity: edu.academyart.model.CourseRole#15164
13 Apr 2005 09:47:36 DEBUG Loader:298 - done processing result set (1 rows)
13 Apr 2005 09:47:36 DEBUG BatcherImpl:211 - done closing: 0 open PreparedStatements, 0 open ResultSets
13 Apr 2005 09:47:36 DEBUG BatcherImpl:275 - closing statement
13 Apr 2005 09:47:36 DEBUG Loader:318 - total objects hydrated: 2
13 Apr 2005 09:47:36 DEBUG SessionImpl:2216 - resolving associations for [edu.academyart.model.AccessPrincipal#3]
13 Apr 2005 09:47:36 DEBUG SessionImpl:2247 - done materializing entity [edu.academyart.model.AccessPrincipal#3]
13 Apr 2005 09:47:36 DEBUG SessionImpl:2216 - resolving associations for [edu.academyart.model.CourseRole#15164]
13 Apr 2005 09:47:36 DEBUG SessionImpl:3994 - creating collection wrapper:[edu.academyart.model.CourseRole.announcementsViewed#15164]
13 Apr 2005 09:47:36 DEBUG SessionImpl:3994 - creating collection wrapper:[edu.academyart.model.CourseRole.examEnrollments#15164]
13 Apr 2005 09:47:36 DEBUG SessionImpl:1996 - loading [edu.academyart.model.User#1]
13 Apr 2005 09:47:36 DEBUG SessionImpl:1996 - loading [edu.academyart.model.Course#2207]
13 Apr 2005 09:47:36 DEBUG SessionImpl:3994 - creating collection wrapper:[edu.academyart.model.UserRole.postsViewed#15164]
13 Apr 2005 09:47:36 DEBUG SessionImpl:1996 - loading [edu.academyart.model.AccessPrincipal#3]
13 Apr 2005 09:47:36 DEBUG SessionImpl:2094 - attempting to resolve [edu.academyart.model.AccessPrincipal#3]
13 Apr 2005 09:47:36 DEBUG SessionImpl:2110 - resolved object in session cache [edu.academyart.model.AccessPrincipal#3]
13 Apr 2005 09:47:36 DEBUG SessionImpl:2247 - done materializing entity [edu.academyart.model.CourseRole#15164]
13 Apr 2005 09:47:36 DEBUG SessionImpl:3161 - initializing non-lazy collections


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 10:02 am 
Beginner
Beginner

Joined: Tue Oct 28, 2003 12:09 pm
Posts: 46
Hmph. RTFM I suppose:

http://www.hibernate.org/hib_docs/refer ... ce-proxies

Can't do that.

sorry for the noise.

:P

-mp


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 1:41 pm 
Newbie

Joined: Thu Apr 07, 2005 12:18 pm
Posts: 6
Location: Toulouse, France
And if you disabled lazy property in UserRole ?
(lazy="false" in the top class, UserRole, nothing in the others)

I've got the same problem (but I used joined-subclass without discriminator) today and I apparently resolved it by setting lazy property to false...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 13, 2005 2:27 pm 
Beginner
Beginner

Joined: Tue Oct 28, 2003 12:09 pm
Posts: 46
Great! That worked.

It means I lose lazy-initing for super class properties, but it's better to not have to deal with the funkyness and to address the possible performance issues later.

Thanks!


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.