-->
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: How to convert HQL to SQL?
PostPosted: Mon Oct 09, 2017 1:29 am 
Newbie

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


Top
 Profile  
 
 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: 1628
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.


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

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:
Code:
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);
        }
    }
}


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.