-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Get sql (HQL) from named query
PostPosted: Mon Sep 17, 2007 4:21 am 
Newbie

Joined: Mon Sep 17, 2007 4:16 am
Posts: 5
Hi all,

I need to get the sql from a named query in order to customize programatically the "where" clause in a transparent way for the final user.

If the named query is written in HQL (JPQL), it's possible to get the generated SQL for it???

Thank you very much


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 7:25 am 
Newbie

Joined: Mon Sep 17, 2007 4:16 am
Posts: 5
Once a javax.persistence.Query is created, how can I extract the generated SQL??Is there a way to obtain a Criteria for the query?

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 20, 2007 4:54 am 
Newbie

Joined: Mon Sep 17, 2007 4:16 am
Posts: 5
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();



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.