Hibernate标准投影没有映射表关联 [英] Hibernate Criteria Projection without mapped association of tables

查看:96
本文介绍了Hibernate标准投影没有映射表关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表格说明


Table1和Table2

现在表格1有3列表示t1,t2,t3,表2有2列t4
和t5。


我必须提取数据从两个表中加入,但在注释或xml中两个表之间没有映射关联。



现在主要问题是我必须使用hibernate投影从两个表中提取选定的列表中的数据表示t1,t2来自Table1,t2来自Table2。



我已经通过互联网,但已经找到关联表的例子。

很高兴如果有任何指导。

解决方案

是的,支持 在Hibernate中。这里唯一的一点是我们必须使用 HQL




可以出现,导致笛卡尔产品或交叉加入。



 来自公式,参数
由公式形式表示,参数为参数

所以在我们的例子中:

  session 
.createQuery(SELECT t1.prop1,t1.prop2,t1.prop3
+t2.prop4,t2.prop5
+FROM Entity1 AS t1,Entity2 As t2+
//如果存在某种关系 - 未映射
+WHERE t1.someProperty = t2.someProperty
+AND ...

.setMaxResults(10)//我们甚至可以在这里页面
.list()

注意:我使用prop1,prop2和Entity1,Entity2 ...来强制这是HQL的感觉。我们正在讨论映射的实体,而不是表或列



我们将收集对象[] array ...


I have 2 tables say

Table1 and Table2

Now Table1 has 3 columns say t1, t2, t3 and Table2 has 2 columns t4 and t5.

I have to fetch data from both tables by join but there is no mapped association between two tables in annotation or xml.

Now main issue is I have to use hibernate projection to fetch selected columns from both tables say t1,t2 from Table1 and t4 from Table2.

I have gone through the internet but have found examples of tables with association.

Glad if there will be any guidance on this.

解决方案

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...

这篇关于Hibernate标准投影没有映射表关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆