-->
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: retrieve super classes and not subclasses with criteria
PostPosted: Wed Mar 08, 2006 1:01 pm 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
Hibernate version:
3

I would like to retrieve all superclasses using a criteria query, but not subclasses. Currently I get twice as much in list since it is the super class, and the subclass, all in one. I would like to limit to just the super class (even though the subclass ISA subclass). Any ideas?

Thanks,
Chris


Top
 Profile  
 
 Post subject: Could you provide details?
PostPosted: Wed Mar 08, 2006 2:04 pm 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
I think there may be a logical problem in your Java inheritance hierarchy - from your question it looks like you have a non-abstract inner node in the inheritance hierarchy, which creates a lot of problems, including the one you're experiencing.

The best way to deal with this issue is to make all inner classes of the hierarchy abstract, and derive concrete leaf classes for what was formerly an inner base, like this:

WAS:
class Widget {
}
class SpecialWidget extends Widget {
}
...
CHANGES TO:
// The "Widget" class becomes AbstractWidget
abstract class AbstractWidget {
}
// A new, nearly empty Widget class is introduced
class Widget extends AbstractWidget {
}
// The base class of the SpecialWidget also changes
class SpecialWidget extends AbstractWidget {
}

With a hierarchy like that you can select from Widget and get only the rows for Widget but not SpecialWidget, because SpecialWidget is no longer a Widget.

I hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 2:12 pm 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
This is a good idea, but I want to reuse logic from one subclass to the other, so it doesnt work. But I did see there is a way, in the class mapping, use polymorphism="explicit", and this is what I want and works.

Thanks though,
Chris


Top
 Profile  
 
 Post subject: Are you sure this is what you need?
PostPosted: Wed Mar 08, 2006 3:51 pm 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
> I want to reuse logic from one subclass to the other, so it doesnt work

It does work - the logic goes into the abstract class, where all shared logic belongs anyway.

> in the class mapping, use polymorphism="explicit", and this is what I want and works.

It's a nice work-around, but it is not a solution unless your inheritance hierarchy fits a very specific description. Explicit polymorphism breaks the object model because it lets you slice your Java objects, presenting an object as its base, which in most cases it isn't. The feature is there for a very specific reason - to implement the "lightweight object" pattern, a work-around to pre-v3 Hibernate's inability to deal with lazy fetching at the attribute level. From the documentation: "Explicit polymorphism is useful when two different classes are mapped to the same table (this allows a "lightweight" class that contains a subset of the table columns)." There are substantial problems with this approach - inability to run "real" polymorphic queries and the need to fiddle with the "evict" calls on the session are only the beginning.

See this article for more information.


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.