-->
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: Query possible: collection contains an instance of a class?
PostPosted: Mon Jan 23, 2006 4:57 am 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
Entity A has ManyToMany relationship with Entity B. Class B is the base class for a class hierarchy, and C is a subclass of B in the class hierarchy.


class A {

List<B> getBList();

}


Query:
get all A(s) that have a instance of C.

Note that: C is a subclass of B. It is OO. If A has an instances of any subclass of C, then the A will be included in result set.

The following code will not work using class, which is exactly match, not OO idea.

List results = session.createCriteria(A.class)
.createCriteria("bList")
.add( Restrictions.eq("class", "B") )
.list();


any help are appreciated. Thanks!

Dave


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 8:51 pm 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
I am looking for something like:

Restrictions.instanceof(object, class);

Hibernate is OO to relational database. I expect it to have this feature. Is it possible with Hibernate? Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 10:06 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
It's definitely possible with HQL (the .class member is provided for this purpose). If there's no equivalent in Criteria (and I couldn't find one) you could explicitly map the discriminator using a function (rather than a column: the column is already in use in your mapping) and add a Criterion for that.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 2:47 am 
Senior
Senior

Joined: Tue Sep 13, 2005 2:01 am
Posts: 137
Thanks!

The .class member in HQL is not OO polymorphic. It requires "Exact" match(simply String comparison)and does not consider inheritance.

I am not quite understanding the mapping function you mentioned. If you can give me an example how to use it with Criteria, it would be a great help.

In my case I am using JOINED mapping strategy, not Single-Table, so there is no discriminator column.

Your help is appreciated.

Thanks.


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.