Skip to content

Instantly share code, notes, and snippets.

@eleumik
Created March 25, 2017 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eleumik/e40abe01dfc93b231f6ed1803b10842b to your computer and use it in GitHub Desktop.
Save eleumik/e40abe01dfc93b231f6ed1803b10842b to your computer and use it in GitHub Desktop.
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import HibernateQueryCompatibility;
/**
* A class used to obtain an {@link org.hibernate.Query}
* or a {@link org.hibernate.SQLQuery} from any version
* of Hibernate.
* <p>Starting from Hibernate 5.2 the Hibernate {@link Session} returns
* a new interface {@link org.hibernate.query.Query}
* that <b>extends</b> the old {@link org.hibernate.Query},
* for this reason binary compatibility is broken:
* code that needs to be reused under any Hibernate version
* should use this {@link HibernateQuery} class
* instead than the Hibernate {@link Session}.
* <p>See also <A href='https://github.com/querydsl/querydsl/issues/1917'>
* https://github.com/querydsl/querydsl/issues/1917</a>.
* @author michele vivoda
*
*/
public final class HibernateQuery
{
private static final HibernateQueryCompatibility provider;
static
{
// custom loader that does not fail if one class fails to load
provider = (HibernateQueryCompatibility) new ServiceLoader(false, HibernateQueryCompatibility.class).getProvider();
}
//public static final Type LONG =provider.getLong();
/**
* A thread safe instance of {@link HibernateQueryCompatibility}.
* @return a {@link HibernateQueryCompatibility}, never <code>null</code>.
*/
public static final HibernateQueryCompatibility getInstance()
{
return provider;
}
/**
* Creates a {@link Query}. See {@link Session#createQuery(String)}.
* @param session a {@link Session}, never <code>null</code>.
* @param hql required HQL
* @return a {@link Query}, never <code>null</code>.
*/
public static Query createQuery(Session session, String hql) {
return provider.createQuery(session, hql);
}
/**
* Creates a {@link SQLQuery}. See {@link Session#createSQLQuery(String)}.
* @param session a {@link Session}, never <code>null</code>.
* @param sql required SQL
* @return a {@link SQLQuery}, never <code>null</code>.
*/
public static SQLQuery createSQLQuery(Session session, String sql) {
return provider.createSQLQuery(session, sql);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment