mysql - 我如何操作嵌套查询 j2ee hibernate

标签 mysql hibernate jpa jakarta-ee hql

我有以下查询,hibernate 不支持在'from' 之后嵌套的问题我试图创建一个 View ,但是没有成功,我想知道如何使用hibernate 正确运行这个查询

SELECT sum(dc.nbrDefaut) def, a.nb control,c.id_of
FROM controlequalite c ,detailscontrole dc,
(select sum(nbreControlle) nb, id_monitrice 
  from controlequalite group by id_monitrice) a
  where c.id = dc.id_controle 
and c.id_monitrice = a.id_monitrice 
and  c.date >= '2016-03-25 00:00:00' 
group by c.id_monitrice,c.id_of;

最佳答案

对于这样的查询,最简单的方法是使用 native 查询。我假设您使用 JPA/Hibernate 作为 ORM,因此您可以简单地执行以下操作:

List<YourObject> resultList = yourEntityManager.createNativeQuery("nativeSQLhere", YourObject.class).getResultList();

关于mysql - 我如何操作嵌套查询 j2ee hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36911208/

相关文章:

mysql - 存在公式减慢查询。需要优化

java - 无法更新 PostgreSQL 表中的字段

spring - 没有 "@Column"注释的 JPA 实体类

java - 如何使用hibernate从多个表查询中获取数据?

java - 使用计数实现 JPA 投影

java - ManyToMany 关系上的 PostgreSQL 约束违规错误,但应存在外键

MySQL 在一个查询中有两个 LAST_INSERT_ID

php - 使用拖放事件中的信息更新 MySQL 数据库

mysql - rails : How to store extra attributes using single table inheritance?

c# - 如何在 NHibernate 中使用子查询作为联接?