Hibernate Criteria Projection without mapped association of tables

こ雲淡風輕ζ 提交于 2019-11-30 10:22:57

Yes, this is supported in Hibernate. The only thing here is that we have to use HQL:

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

from Formula, Parameter
from Formula as form, Parameter as param

So in our case:

session
   .createQuery("SELECT t1.prop1, t1.prop2, t1.prop3 "
              + "       t2.prop4, t2.prop5 "
              + " FROM Entity1 AS t1, Entity2 As t2 " +
              // if there is some relation - unmapped
              + " WHERE t1.someProperty = t2.someProperty "
              + "   AND ... "
              )
   .setMaxResults(10) // we can even page here
   .list() 

NOTE: I used prop1, prop2 and Entity1, Entity2 ... to force the feeling that this is HQL. We are talking about mapped entities, not tables or columns

And we will recieve collection of object[] array...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!