Hi again,
I'm able to get the SQL from an HQL named query. However, the generated SQL contains question marks instead of the named parameter value. How can I fix it?
This is the HQL:
Code:
SELECT DISTINCT p FROM CargoAcademico AS ca, IN (ca.persona) p WHERE :fecha BETWEEN ca.vigencia.start AND ca.vigencia.end
This is the generated SQL:
Code:
select distinct persona1_.id as col_0_0_ from cargo_academico cargoacade0_ inner join persona persona1_ on cargoacade0_.persona_id=persona1_.id where ? between cargoacade0_.fecha_activacion and cargoacade0_.fecha_desactivacion
And this is my code:
Code:
// Retrieve the JPA EntityManager using Spring
EntityManager em =
getJpaTemplate().getEntityManagerFactory().createEntityManager();
// Hibernate Session
org.hibernate.Session session = (Session) em.getDelegate();
SessionFactoryImplementor sessionFactory =
(SessionFactoryImplementor) session.getSessionFactory();
// javax.persistence.Query vS org.hibernate.ejb.QueryImpl vS
// org.hibernate.Query
Query query =
em
.createNamedQuery(AclJpaDepartamentosDao.BUSCAR_PERSONAS_OCUPADAS);
query.setParameter("date", new Date());
org.hibernate.ejb.QueryImpl queryImpl =
(org.hibernate.ejb.QueryImpl) query;
org.hibernate.Query hqlQuery = queryImpl.getHibernateQuery();
// Obtener la cadena de la HQL
String hqlQueryString = hqlQuery.getQueryString();
// Obtener el QueryTranslator
org.hibernate.hql.ast.ASTQueryTranslatorFactory queryTranslatorFactory =
new org.hibernate.hql.ast.ASTQueryTranslatorFactory();
org.hibernate.hql.QueryTranslator queryTranslator =
queryTranslatorFactory.createQueryTranslator(
AclJpaDepartamentosDao.BUSCAR_PERSONAS_OCUPADAS,
hqlQueryString, new HashMap(), sessionFactory);
// Obtener la cadena SQL
queryTranslator.compile(new HashMap(), true);
String sqlQueryString = queryTranslator.getSQLString();