How to convert HQL to SQL Query programmatically (without logging)

后端 未结 7 1160
刺人心
刺人心 2021-02-05 12:10

I am executing the following HQL and it is executing properly

String hql = \"FROM Employee\";
Query query = session.createQuery(hql);
List results = query.list(         


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-05 12:34

    This is also possible with TypedQuery in later versions of Hibernate using the following code

    String hqlQueryString=typedQuery.unwrap(org.hibernate.query.Query.class).getQueryString();
            ASTQueryTranslatorFactory queryTranslatorFactory = new ASTQueryTranslatorFactory();
            SessionImplementor hibernateSession = entityManager.unwrap(SessionImplementor.class);
            QueryTranslator queryTranslator = queryTranslatorFactory.createQueryTranslator("", hqlQueryString, java.util.Collections.EMPTY_MAP, hibernateSession.getFactory(), null);
            queryTranslator.compile(java.util.Collections.EMPTY_MAP, false);
            String sqlQueryString = queryTranslator.getSQLString();
    

提交回复
热议问题