All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.hypersistence.utils.hibernate.query.SQLExtractor Maven / Gradle / Ivy

There is a newer version: 3.7.0
Show newest version
package io.hypersistence.utils.hibernate.query;

import io.hypersistence.utils.hibernate.util.ReflectionUtils;
import org.hibernate.Filter;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.AbstractQueryImpl;

import javax.persistence.Query;
import java.util.Collections;

/**
 * The {@link io.hypersistence.utils.hibernate.query.SQLExtractor} allows you to extract the
 * underlying SQL query generated by a JPQL or JPA Criteria API query.
 * 

* For more details about how to use it, check out this article on vladmihalcea.com. * * @author Vlad Mihalcea * @since 2.9.11 */ public class SQLExtractor { protected SQLExtractor() { } /** * Get the underlying SQL generated by the provided JPA query. * * @param query JPA query * @return the underlying SQL generated by the provided JPA query */ public static String from(Query query) { AbstractQueryImpl abstractQuery = query.unwrap(AbstractQueryImpl.class); SessionImplementor session = ReflectionUtils.getFieldValue(abstractQuery, "session"); String[] sqls = session.getFactory() .getQueryPlanCache() .getHQLQueryPlan(abstractQuery.getQueryString(), false, Collections.emptyMap()) .getSqlStrings(); return sqls.length > 0 ? sqls[0] : null; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy