开发者

using multiple table in hibernate, problem with output

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?


select a from A as a 
join fetch B as b 
join fetch C as c 
where a.val='1' 
and b.val=a.val 
and c.val=b.val

You can use join.

return List<A>

you can use a.getB().get.... to get your data.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜