-->
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.  [ 5 posts ] 
Author Message
 Post subject: Query by Superclass - return any subclasses
PostPosted: Fri Dec 29, 2006 7:46 am 
Newbie

Joined: Fri Nov 10, 2006 12:41 pm
Posts: 3
Is it not possible to query by superclass?
ie if I have superclass that may or may not be persistent, may or may not be abstract and a bunch of concrete persistent subclasses.

(eg in a single table per hierachy where MyTop maps to the single table)

Code:
              MyTop
            /      \
      MyMiddle    ClassA
    /    |    \
ClassX ClassY ClassZ


Can I not say:

select x.id from MyTop x where x.class = MyMiddle

or possibly

select x.id from MyTop x where subclassOf(MyMiddle)

returning the ids of any persisted objects of classes: MyMiddle.class, ClassX.class, ClassY.class, ClassZ.class

Is there no mechanism to achieve this? I'm resorting to a boolean flag when the information is already there in the discriminator. Surely hibernate can construct the necessary SQL ie:

Where discriminator='MM' or discriminator='X' or discriminator='Y' or discriminator='Z'

If I have to do that then my code is not future proof when I add more subclasses.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 30, 2006 4:53 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
If I get you well, Hibernate can do polymorphic query simply. For example, there's a special HQL query which is amazing to test : "from Object" will retrieve all the dabase :-).

So for your case, if you want to retrieve all classes and subclasses of one given class, say MyClass, just do this:
Code:
List list = session.createQuery("from MyClass").list();

If you want to restrict the result, do it in the where, quite like in sql. Maybe have a look in the reference documentation: http://www.hibernate.org/hib_docs/v3/re ... lymorphism

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 30, 2006 11:26 am 
Newbie

Joined: Fri Nov 10, 2006 12:41 pm
Posts: 3
batmat wrote:
If I get you well, Hibernate can do polymorphic query simply. For example, there's a special HQL query which is amazing to test : "from Object" will retrieve all the dabase :-).

So for your case, if you want to retrieve all classes and subclasses of one given class, say MyClass, just do this:
Code:
List list = session.createQuery("from MyClass").list();

If you want to restrict the result, do it in the where, quite like in sql. Maybe have a look in the reference documentation: http://www.hibernate.org/hib_docs/v3/re ... lymorphism


I have studied the docs and even have the new book.

I cannot do 'select from MyMiddle' since MyMiddle may not be a persistent entity. It seems that I can only query the highest superclass ie the one mapped to the table.

Perhaps I'l just have to make the class persistent for the sake of querying.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 31, 2006 5:20 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, I also have the new book and I'd be interesting in knowing where you read that. I guess it depends on the type of query you want to do, because the HQL query "from MyMiddle" would indeed return every persistent instances in the db that inherits (directly or under many levels) MyMiddle.

Maybe this is only projection which is not supported. I don't remember and can't retest it now. But since "from Object" is valid, asserting that querying by a non mapped superclass is not possible is simply not true, I think.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 7:21 am 
Beginner
Beginner

Joined: Thu Nov 02, 2006 9:38 am
Posts: 32
Location: Belgium
The manual specifically states that you can even query on interface names. And interfaces surely aren't persisted.


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