-->
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.  [ 3 posts ] 
Author Message
 Post subject: Inheritance Problem with interface
PostPosted: Thu Jun 29, 2006 4:21 pm 
Newbie

Joined: Wed Apr 05, 2006 4:33 pm
Posts: 5
I have code *similar* to the following

Code:
   public interface TheInterface
   {
      
   }
   
   @Entity
   @Inheritance(strategy = InheritanceType.JOINED)
   public abstract class AbstractClass
   {
      @Id
      public int id;
                abstract void foo();
      
   }
   
   @Entity
   public class DerivedClass1 extends AbstractClass implements TheInterface
   {
                void foo() {}
   }
   
   @Entity
   public class DerivedClass2 extends AbstractClass
   {
                void foo() {}
   }
   
   public class Main
   {
      public static void main(String args[])
      {
         Session session = HibernateUtil.getSession();
         DerivedClass1 dc1 = new DerivedClass1();
         session.save(dc1);
         List<AbstractClass> list = session.createQuery("from AbstractClass").list();
         if(!(list.get(0) instanceof TheInterface))
            throw RuntimeException("What the hell is going on here?");
      }
   }



Now, the weird thing is that I'm gettting the RuntimeException!
Debuging into it shows me that the runtime type of list.get(0) is a cglib enhanced version of AbstractClass, which should be impossible, since it's abstract!

Hibernate version:
3.1, with annotations 3.1beta9

Mapping documents:
Annotations, no mapping

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

Full stack trace of any exception that occurs:

Name and version of the database you are using:
MySQL, version 5

The generated SQL (show_sql=true):
N/A?

Debug level Hibernate log excerpt:
N/A?

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 1:42 pm 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
A CGLIB proxy extends your orginal class, which most likely how Hibernate is able to instantate an abstract class. This page of the Hibernate docs expliains this further:

http://www.hibernate.org/hib_docs/v3/re ... ng-proxies

Now I'm not sure how annotations changes the game, but you generally cannot cast a CGLIB proxy to a super-type. To get around this, you'd define a proxy interface. With annotations, it's not clear if Hibernate uses the interface implicitly, or if there is another annotation to declare a proxy iinterface.

Just curious though, what type of RuntimeException is actually thrown when you do your instanceof test?

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 01, 2006 11:55 am 
Newbie

Joined: Wed Apr 05, 2006 4:33 pm
Posts: 5
I saw that I can add @Proxy to a class, but I'm not sure what that does. Adding @proxy to either of my classes didn't work. I have a workaround right now which is simple: I changed my instance from LAZY to EAGER fetch type and that made it so that I no longer get the CGLIB proxy, In this particular case I'm not concerned because I know I'll need these objects immediately anyway.

I'd like to know the better solution anyway, but for now it's working.
The runtime exception? Oh, that's just my code's way of saying that I didn't get what I expected (look at my original sample code)

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.