我有 2 张 table
Table1 and Table2
Now Table1 has 3 columns say t1, t2, t3 and Table2 has 2 columns t4 and t5.
我必须通过联接从两个表中获取数据,但注释或 xml 中的两个表之间没有映射关联。
现在的主要问题是我必须使用 hibernate 投影从两个表中获取选定的列,例如来自 Table1 的 t1、t2 和来自 Table2 的 t4。
我浏览过互联网,但找到了具有关联的表格的示例。
很高兴如果对此有任何指导。
最佳答案
是的,Hibernate 支持。这里唯一的事情是我们必须使用HQL:
- 16.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
所以在我们的例子中:
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()
注意:我使用 prop1、prop2 和 Entity1、Entity2 ... 来强制感觉这是 HQL。我们正在讨论映射的实体,而不是表或列
我们将收到object[]
数组的集合...
关于没有表的映射关联的 Hibernate 标准投影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25679783/