views:

27

answers:

2

I am using following query

from A as a,B as b,C as c where a.val='1' and b.val=a.val and c.val=b.val

Now if i do follow

query.list();

what will be the output?

Also if I want to get the output of above query what else I have to do?

A: 

Hi this is not the right way.For this you have to write nested query Using Criteria.That will be something like as follows:

 String query="from A a where a.val in (select b.val from B b where b.val in (select c.val from C c where c.val='1'))";    
 Criteria criteria = session.getSession().createCriteria(TableName.class);
        criteria.add(Restrictions.sqlRestriction(query));
   List<tableNameObj> tableNameObj=criteria.list();

Hopefully this will solve your problem.

Rupeshit
this query will not provide me data from table B but i require data form table B and C also to be Listed with Table A... so i think this query will not work
Ohh....then the query which you write is perfect.It is might be giving you null result as one of the table from A,B or C doest not having that record.
Rupeshit
the query is running very fine its giving me the output as object[] with objects of A,B,C and all these tables are mapped with foreign key there is not chance of null.....
A: 

From the Hibernate Reference Documentation:

14.2. The from clause

...

Multiple classes can appear, resulting in a cartesian product or "cross" join.

from Formula, Parameter

from Formula as form, Parameter as param

So your query will return a cartesian product and if there are X matching rows in A, Y matching rows in B, Z matching rows in C, you'll get X * Y * Z results (and the product can become huge very quickly).

Be very careful with this kind of queries, they are very greedy with resources and might cause serious performance problems.

Pascal Thivent
how i can improve such query's please help...
@abhishekgem84: Please explain your goal in pure english and maybe I'll be able to help.
Pascal Thivent