Languages
[Edit]
EN

Hibernate - get HQL from TypedQuery

4 points
Created by:
JustMike
29670

In this article, we would like to show how to get HQL query from TypedQuery (from CriteriaQuery) in Hibernate.

Quick solution:

// import javax.persistence.EntityManager;
// import javax.persistence.TypedQuery;
// import javax.persistence.criteria.CriteriaBuilder;
// import javax.persistence.criteria.CriteriaQuery;
// import org.hibernate.query.Query

// import com.example.MyEntity;


EntityManager entityManager = ...; // to know how to get it check below examples

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<MyEntity> criteriaQuery = criteriaBuilder.createQuery(MyEntity.class);

// query code here ...

TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery);
Query hibernateQuery = typedQuery.unwrap(Query.class);

String hqlQuery = hibernateQuery.getQueryString(); //  <--------------------------- HQL query

System.out.println(hqlQuery); // e.g. select generatedAlias0 from MyEntity as generatedAlias0

 

Practical example

UsersController.java file:

package com.example;

import logic.database.entity.crawler.CrawlerJobOfferDetailEntity;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.example.HqlUtils;

@Controller
public class UsersController {
    
    @PersistenceContext
    private EntityManager entityManager;

    @RequestMapping(
            value = "/api/users/hql-query",
            method = RequestMethod.GET,
            produces = MediaType.TEXT_PLAIN_VALUE
    )
    @ResponseBody
    @Transactional
    public String getHqlQuery() {

        CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
        CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
        Root<UserEntity> rootPath = criteriaQuery.from(UserEntity.class);

        criteriaQuery.select(criteriaBuilder.count(rootPath));

        String hqlQuery = HqlUtils.renderHql(entityManager, criteriaQuery);
        System.out.println(hqlQuery); // select count(generatedAlias0) from UserEntity as generatedAlias0
        
        return hqlQuery;
    }
}

HqlUtils.java file:

package com.example;

import org.hibernate.query.Query;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;

public class HqlUtils {

    private HqlUtils() {
        // nothing here ...
    }

    public static String renderHql(Query hibernateQuery) {
        return hibernateQuery.getQueryString();
    }

    public static <T> String renderHql(EntityManager entityManager, CriteriaQuery<T> criteriaQuery) {
        TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery);
        Query hibernateQuery = typedQuery.unwrap(Query.class);

        return renderHql(hibernateQuery);
    }
}
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join