-->
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.  [ 7 posts ] 
Author Message
 Post subject: Querying the instances of a class.
PostPosted: Thu Jul 05, 2007 2:02 pm 
Newbie

Joined: Tue Jul 03, 2007 2:06 pm
Posts: 14
Location: Turkey, Ankara
Is there a way of querying the class list for finding the instances of a specific class.

For example

lets say

class abstract animal
{
....
}

class dog:animal
{
...
}

class cat:animal
{
.....
}

i want to find in nhibernate context the classes implementing animal which should be

{dog,cat}

_________________
it is what they understand from your sayings, not what u know.

Fatih Kucukpetek


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 3:06 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Code:
session.CreateCriteria( typeof( animal ) ).List();
or
Code:
session.CreateQuery( "from animal" ).List();
will return all dogs and cats (animals).

Code:
session.CreateCriteria( typeof( dog ) ).List();
or
Code:
session.CreateQuery( "from dog" ).List();
will return all dogs.

Of course, all this assumes the inheritance mappings are correctly structured. Was there something more specific you wanted to do?


Top
 Profile  
 
 Post subject: i need the class name not the instances of them
PostPosted: Thu Jul 05, 2007 3:14 pm 
Newbie

Joined: Tue Jul 03, 2007 2:06 pm
Posts: 14
Location: Turkey, Ankara
I want to list the class names, not the instances of the classes.

the specified query will return
{Dog1, Dog2, Cat1, Cat2}

thanks in advance

_________________
it is what they understand from your sayings, not what u know.

Fatih Kucukpetek


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 05, 2007 4:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Calling ISessionFactory.GetAllClassMetadata() will return an IDictionary. The keys of the IDictionary are all the Types that NHibernate is aware of. You can do something like this:
Code:
IDictionary dict = sessionFactory.GetAllClassMetadata();
foreach (Type type in dict.Keys) {
    if (type.IsSubclassOf(typeof(Animal))) {
        // do whatever
    }
}

_________________
Karl Chu


Top
 Profile  
 
 Post subject: Yes it solved the problem
PostPosted: Fri Jul 06, 2007 2:29 pm 
Newbie

Joined: Tue Jul 03, 2007 2:06 pm
Posts: 14
Location: Turkey, Ankara
i think there should be another way to implement this since if the object traversal may be costly if the classes in the context is much.

_________________
it is what they understand from your sayings, not what u know.

Fatih Kucukpetek


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 06, 2007 2:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Let us know if you find another way. I, for one, would be interested. Thanks.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 07, 2007 3:24 am 
Newbie

Joined: Tue Jul 03, 2007 2:06 pm
Posts: 14
Location: Turkey, Ankara
Karl thanks again,
i looked for other possible solutions. Also i looked for the solution you have decided, i found that the solution u provided is not so costly. Providing that if any other solution is there it should use much more deeper reflection.
thanks again.

_________________
it is what they understand from your sayings, not what u know.

Fatih Kucukpetek


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