HQL a SQL en Java 5

Is it possible to translate in run time, the HSQL statement to SQL using EntityManager?

I've found this solution but I don't have the sessionFactory, I have EntityManager

protected String toSql(String hqlQueryText) {
    if (sessionFactory != null && hqlQueryText != null && hqlQueryText.trim().length() > 0) {
        final QueryTranslatorFactory translatorFactory = new ASTQueryTranslatorFactory();
        final SessionFactoryImplementor factory = (SessionFactoryImplementor) sessionFactory;
        final QueryTranslator translator = translatorFactory.createQueryTranslator(hqlQueryText, hqlQueryText,
        Collections.EMPTY_MAP, factory);
        translator.compile(Collections.EMPTY_MAP, false);
        return translator.getSQLString();
        }
    return null;
}

preguntado el 31 de julio de 12 a las 13:07

1 Respuestas

If you got the EntityManager you can use the following piece of code to get the SessionFactory:

EntityManager em = ...
SessionFactory sf = ((EntityManagerFactoryImpl)em.getEntityManagerFactory()).getSessionFactory();

Respondido 31 Jul 12, 16:07

Sorry but I don't find getEntityManagerFactory in the EntityManager API docs.oracle.com/javaee/5/api/javax/persistencia/… - Javi Pedrera

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.