当两者之间没有FK/PK关系时,我需要在属性上加入两个JPA实体.我正在使用Hibernate,可以像这样使用HQL查询
select foo, bar from FooEntity as foo, BarEntity as bar
where foo.someothercol = 'foo' and foo.somecol = bar.somecol
Run Code Online (Sandbox Code Playgroud)
但是,我想避免依赖Hibernate并使用EntityManager.请帮忙.
Mik*_*unu 19
您的查询是有效的JPQL,并且不使用Hibernate特定的功能(只缺少bar和from之间的空格).在JPA 2.0规范(4.4.5联接)中,用以下单词解释:
可以通过在FROM子句中使用笛卡尔积和在WHERE子句中使用连接条件来隐式指定内连接.在没有连接条件的情况下,这会减少为笛卡尔积.
此通用连接样式的主要用例是连接条件不涉及映射到实体关系的外键关系.示例:SELECT c FROM Customer c,
员工e WHERE c.hatsize = e.shoesize
与查询的主要区别在于您的选择包含两种类型的实体.查询结果是List of Object [].数组中的元素顺序与select语句中的相同.以下是您的案例:
String query =
"select foo, bar from FooEntity as foo, BarEntity as bar "+
"where foo.someothercol = 'foo' and foo.somecol = bar.somecol";
List<Object[]> results = em.createQuery(query).getResultList();
for (Object[] fooAndBar: results) {
FooEntity foo = (FooEntity) fooAndBar[0];
BarEntity bar = (BarEntity) fooAndBar[1];
//do something with foo and bar
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30600 次 |
| 最近记录: |