org.balisunrise.daa.hibernate.HQuery.java Source code

Java tutorial

Introduction

Here is the source code for org.balisunrise.daa.hibernate.HQuery.java

Source

/*
 * Copyright (C) 2015 Glauco Knihs. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Please contact Glauco Knihs, Rua 10 de Junho, N469, Centro, 
 * Guabiruba-SC, CEP 88360-000, BRAZIL, eglauko@hotmail.com, 
 * if you need additional information or have any questions.
 */

package org.balisunrise.daa.hibernate;

import java.util.List;
import org.balisunrise.daa.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;

/**
 * Classe HQuery.
 *
 * @author Glauco Knihs
 * @version 1.0
 * @param <T> Tipo de dado da entidade a ser executada a query.
 * @since 1.0
 */
public class HQuery<T> implements Query<T> {

    //private Session session;
    //private Class<T> entityClass;

    private org.hibernate.Criteria hcrit;
    private Criteria<T> qcrit;

    private HAliasMap aliasMap;

    private int firstResult;
    private int maxResults;

    public HQuery(Session session, Class<T> entityClass) {
        //this.session = session;
        //this.entityClass = entityClass;
        hcrit = session.createCriteria(entityClass);
        aliasMap = new HAliasMap(hcrit);
        qcrit = new HCriteria(this, hcrit, aliasMap);
    }

    @Override
    public int getFirstResult() {
        return this.firstResult;
    }

    @Override
    public void setFirstResult(int firstResult) {
        this.firstResult = firstResult;
        hcrit.setFirstResult(firstResult);
    }

    @Override
    public int getMaxResults() {
        return maxResults;
    }

    @Override
    public void setMaxResults(int maxResults) {
        this.maxResults = maxResults;
        hcrit.setMaxResults(maxResults);
    }

    @Override
    public Criteria<T> getCriteria() {
        return qcrit;
    }

    @Override
    public List<T> list() {
        return hcrit.list();
    }

    @Override
    public T first() {
        return (T) hcrit.uniqueResult();
    }

    @Override
    public boolean any() {
        return hcrit.setProjection(Projections.id()).uniqueResult() != null;
    }

    @Override
    public long count() {
        return (long) hcrit.setProjection(Projections.rowCount()).uniqueResult();
    }

    @Override
    public <V> V min(String property) {
        return (V) hcrit.setProjection(Projections.min(property)).uniqueResult();
    }

    @Override
    public <V> V max(String property) {
        return (V) hcrit.setProjection(Projections.max(property)).uniqueResult();
    }

    @Override
    public <V> V avg(String property) {
        return (V) hcrit.setProjection(Projections.avg(property)).uniqueResult();
    }

    @Override
    public <V> V sum(String property) {
        return (V) hcrit.setProjection(Projections.sum(property)).uniqueResult();
    }

    @Override
    public <V> List<V> ids() {
        return (List<V>) hcrit.setProjection(Projections.id()).list();
    }

}