two tables subconsultas parametros lista fechas ejemplos datos consultas consultar con hibernate jpa subquery

tables - lista hibernate



Subconsulta de JPA/hibernación en la cláusula from (2)

Estamos utilizando JPA con hibernate como proveedor, tenemos una consulta que contiene una combinación con una subconsulta en la cláusula FROM, pero obtenemos el siguiente error:

org.hibernate.hql.ast.QuerySyntaxException: token inesperado: (cerca de la línea 1, columna 75 [SELECCIONAR sd FROM com.hp.amber.datamodel.entities.analysis.SnapshotDates sd, (SELECT max (x.changeDate) maxChangeDate, x .viewId, x.state FROM com.hp.amber.datamodel.entities.analysis.SnapshotDates x WHERE x.changeDate <: date AND x.viewId in (: viewIds) AND x.state =: state GROUP BY x.viewId, x.state) sd2 WHERE sd.viewId = sd2.viewId AND sd.state =: state Y sd.changeDate = sd2.maxChangeDate]

Esta es la consulta:

SELECT sd FROM SnapshotDates sd, (SELECT max(x.changeDate) maxChangeDate, x.viewId, x.state FROM SnapshotDates x WHERE x.changeDate<:date AND x.viewId in (:viewIds) AND x.state=:state GROUP BY x.viewId, x.state) sd2 WHERE sd.viewId = sd2.viewId AND sd.state = :state AND sd.changeDate = sd2.maxChangeDate

Gracias por ayudar


Tu SQL es:

SELECCIONAR sd FROM SnapshotDates sd, (SELECT max (x.changeDate) maxChangeDate, x.viewId, x.state FROM SnapshotDates x WHERE x.changeDate <: date AND x.viewId in (: viewIds) AND x.state =: state GROUP POR x.viewId, x.state) sd2 WHERE sd.viewId = sd2.viewId AND sd.state =: state Y sd.changeDate = sd2.maxChangeDate

Puede reescribir su sql como

SELECT sd FROM SnapshotDates sd, WHERE sd.viewId in (:viewIds) AND sd.state = :state sd.changeDate = (SELECT max(x.changeDate) FROM SnapshotDates x WHERE x.viewId = ds.viewId AND x.state = ds.state)

Encontrar inspirado por el ejemplo

SELECT m FROM Professor m WHERE (SELECT COUNT(e) FROM Professor e WHERE e.manager = m) > 0

http://www.java2s.com/Code/Java/JPA/EJBQLWhereClauseWithSubQuery.htm

Mi ejemplo similar tuve SQL

select k.* from kredits k, (select client_id, max(r_date) r_date from kredits k group by client_id) k2 where k.client_id = k2.client_id AND k.r_date = k2.r_date order by k.id

Reescríbalo para PQL

select k From Kredit k where k.rDate = (select MAX(k2.rDate) from Kredit k2 where k2.clientId = k.clientId) order by k.id

Será traducido a

select kredit0_.id as id28_, kredit0_.client_id as client59_28_ from kredits kredit0_ where kredit0_.r_date=(select MAX(kredit1_.r_date) from kredits kredit1_ where kredit1_.client_id=kredit0_.client_id) order by kredit0_.id

devuelve el mismo resultado que SQL.

Utilice Hebirnate 3.3.1 con MySQL 5.0.24