-->
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: Equals Proxies and Polymorphism
PostPosted: Tue Sep 21, 2004 12:45 pm 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
Just want to have some clear ideas on proxies and polymorphic classes.

It seems there is some problem using proxies with polymorphic class. If I load a base class proxy, I cannot cast it to the subclass even if the underlying instance is an instance of the subclass.

In my case, this breaks my equals method because I do an instanceof check in it.

I understand why, but I just want to know the recommended workaround. Does that mean that I must not use proxies with polymorphic classes (or that it is not recommended).

If this is the solution I just want to raise a possible problem with the next version of hibernate. On the dev mailing list gavin said that lazy will be the default behaviour for classes in this next version. This could lead in confusing bugs for people who did not uses proxies in the past and will see strange equals behaviour on their polymorphic classes in the future.

seb


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 21, 2004 1:02 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
I'm using proxies with polymorphic objects and haven't had the trouble you describe. I have seen some problems when querying the data using HQL. Polymorphic queries are discussed in the reference manual, albeit quite lightly:

http://www.hibernate.org/hib_docs/reference/en/html_single/#queryhql-polymorphism

Probably the difficulty lies in the fact that you've got a proxy on the base class. Try not defining a proxy for the base class and ONLY put the proxy on the sub-classes. The reason is that when class enhancement occurs, it creates a descendent of the enhanced class (no magic here, just plain old sub-classing). That enhanced class would be a sibling of your sub-classes, not an ancestor.

In order to get the polymorphism right you also might have to experiment with explicit polymorphism, described in:

http://www.hibernate.org/hib_docs/refer ... tion-class

under implicit vs. explicit polymorphism (do a search).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 22, 2004 2:28 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
With this kind of relation I cannot avoid proxy on the base class. This is the situation where I got the problem

Code:
public class Person
{
  private Cat myCat;
}
public class Cat
{
}
public class DomesticCat extends Cat
{
}


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.