Hibernate Books

All times are UTC - 5 hours [ DST ]

Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How to convert HQL to SQL?
PostPosted: Mon Oct 09, 2017 1:29 am 

Joined: Sat Oct 07, 2017 3:45 am
Posts: 5
How to convert HQL to SQL ? Is there ant tool for that ?

 Post subject: Re: convert HQL to SQL
PostPosted: Mon Oct 09, 2017 2:42 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1588
Location: Romania
It's done automatically by Hibernate. If you have an HQL query and want to know what's the associated SQL query, justs let it execute and print it to the logs.

Check out this article for more details.

If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.

 Post subject: Re: How to convert HQL to SQL?
PostPosted: Mon Oct 09, 2017 4:55 am 

Joined: Sun May 08, 2005 12:26 pm
Posts: 10
If you want to translate HQL programmatically to SQL you can do it this way:
import java.util.Collections;
import javax.persistence.EntityManagerFactory;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.hibernate.MappingException;
import org.hibernate.QueryException;
import org.hibernate.engine.jdbc.internal.FormatStyle;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory;
import org.hibernate.hql.spi.QueryTranslator;
import org.hibernate.hql.spi.QueryTranslatorFactory;

public class HqlTranslator {

    private final SessionFactoryImplementor sessionFactoryImplementor;
    private final QueryTranslatorFactory translatorFactory;

    public HqlTranslator(EntityManagerFactory entityManagerFactory) {
        sessionFactoryImplementor = entityManagerFactory.unwrap(SessionFactoryImplementor.class);
        translatorFactory = new ASTQueryTranslatorFactory();

    public String translate(String hql, boolean format) {
        if (hql == null || hql.trim().length() == 0) {
            return "";
        try {
            final QueryTranslator translator
                    = translatorFactory.createQueryTranslator(
                            hql, hql, Collections.EMPTY_MAP,
                            sessionFactoryImplementor, null);
            translator.compile(Collections.EMPTY_MAP, false);
            final String sqlString = translator.getSQLString();
            return format
                    ? FormatStyle.BASIC.getFormatter().format(sqlString)
                    : sqlString;
        } catch (QueryException | MappingException t) {
            return ExceptionUtils.getStackTrace(t);

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.