views:

248

answers:

4

I have a question on criteria: How can i use a Criteria (or similar) that filters and/or do whatever with another criteria?

Something like:

select clients.* from (select * from clients) as clients

The real problem is something else, but achieving this behaviour would be terrific...

(btw, both java and .net are welcome to help)

thanks

+1  A: 

Your FROM clause needs to be a mapped object. You could do a subselect inside the WHERE clause... something like:

select c from clients c where c.id in (select c2.id from clients c2)

It would help if you could give a better example. The example you gave can be reduced down to the following HQL:

"from clients"

...which isn't terribly useful.

Civil Disobedient
lol, thank you. My problem is that this is a quite hard query to put on nhibernate common standarts...Perhaps i need to study a little more, to come up with a better solution.
NoProblemBabe
+2  A: 

It can't be done, AFAIK. The tutorial about HQL says:

Note that HQL subqueries can occur only in the select or where clauses.

I can't find same statement about Criteria, but in API the only way to create criteria is to give mapped type. There is support for subqueries but only in where clause. Here is javadoc.

Tadeusz Kopec
In fact, the thing was that I wanted to use it on the FROM clause, like a result set, such as with (tsql).
NoProblemBabe
A: 

i'm not sure i understand your question correctly, but if you want to do a select on a list of objects, you can use the subqueries with DetachedCriteria. I use it all the time, especially for paging objects while creating a left outer join, which could lead me to incorrect number of entities.

Imagine you've got users who buy products, with a relationship many-many:

Dim dc As DetachedCriteria = DetachedCriteria.For(GetType(User)).SetFirstResult(pageNumber * itemsPerPage).SetMaxResults(itemsPerPage) Session.CreateCriteria(GetType(user)).Add(Subqueries.PropertyIn("Id", dc)).CreateAlias("ProductsBought", "pb", NHibernate.SqlCommand.JoinType.LeftOuterJoin)

Thom's right, perhaps you should be more precise...

samy
Hibernate supports subqueries in SELECT and WHERE caluse. The OP is about subquery in FROM clause. It can't be done AFAIK.
Tadeusz Kopec
Unfortunatelly he's right...My intentions were to use on the from clause...
NoProblemBabe
+1  A: 

You could try adding a NHibernate.Criterion.InExpression to your criteria.

Found an example on this blog: http://www.lostechies.com/blogs/jimmy%5Fbogard/archive/2008/08/26/parameter-lists-in-nhibernate.aspx

Ben F
Great escape, thank you very, very much
NoProblemBabe