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

org.bitbucket.brunneng.qb.HibernateQueryBuilder Maven / Gradle / Ivy

Go to download

Compact tool for building SQL, JPA or hibernate queries. Supports utilities for spring pagination and sorting. Supports extending to other types of query languages. Has no transitive dependencies - use only what you need.

The newest version!
package org.bitbucket.brunneng.qb;

import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;

import javax.persistence.TypedQuery;

/**
 * HQL Query builder which uses hibernate {@link Session} to build queries.
 */
public class HibernateQueryBuilder extends AbstractJpqlQueryBuilder {
   private final Session session;

   public HibernateQueryBuilder(Session session) {
      this.session = session;
   }

   /**
    * Builds hql query with applied parameters
    * @return hql query ready to be executed
    */
   public Query build() {
      return (Query)super.build();
   }

   /**
    * Builds typed hql query with applied parameters
    * @return typed hql query ready to be executed
    */
   public  Query build(Class resultClass) {
      return (Query) super.build(resultClass);
   }

   /**
    * Builds native query with applied parameters
    * @return native query ready to be executed
    */
   public NativeQuery buildNative() {
      return (NativeQuery) super.buildNative();
   }

   /**
    * Builds hql query to get a count of objects, selected by current query.
    * For example if your query is `select o from Order o where ...`, then built query will be
    * `select count(o) from Order o where ...`.
    * @return hql count query, to be executed
    */
   public Query buildCountQuery() {
      return (Query) super.buildCountQuery();
   }

   /**
    * Builds native query to get a count of objects, selected by current query.
    * For example if your query is `select o.id from orders o where ...`, then built query will be
    * `select count(o.id) from orders o where ...`.
    * @return native count query, to be executed
    */
   public NativeQuery buildCountNativeQuery() {
      return (NativeQuery) super.buildCountNativeQuery();
   }

   @Override
   protected javax.persistence.Query createQuery(QueryWithParams queryWithParams) {
      javax.persistence.Query query = session.createQuery(queryWithParams.getQuery());
      applyParametersToQuery(query, queryWithParams.getParameters());
      return query;
   }

   @Override
   protected  TypedQuery createTypedQuery(QueryWithParams queryWithParams, Class targetClass) {
      TypedQuery query = session.createQuery(queryWithParams.getQuery(), targetClass);
      applyParametersToQuery(query, queryWithParams.getParameters());
      return query;
   }

   @Override
   protected javax.persistence.Query createNativeQuery(QueryWithParams queryWithParams) {
      javax.persistence.Query query = session.createNativeQuery(queryWithParams.getQuery());
      applyParametersToQuery(query, queryWithParams.getParameters());
      return query;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy