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

com.github.aqiu202.starters.jpa.sql.BaseQuery Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package com.github.aqiu202.starters.jpa.sql;


import com.github.aqiu202.starters.jpa.sql.trans.MapBeanTransformer;
import com.github.aqiu202.starters.jpa.sql.trans.inter.ChangeableTransformer;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.transform.Transformers;
import org.springframework.data.domain.Pageable;

@SuppressWarnings({"unchecked"})
public class BaseQuery {

    protected Query query;
    protected Query countQuery;
    protected ResultTransformer mapTransformer = MapBeanTransformer.INSTANCE;
    protected ChangeableTransformer beanTransformer;
    protected Pageable pageable;

    protected BaseQuery() {
    }

    protected BaseQuery(ChangeableTransformer changeableTransformer) {
        this.beanTransformer = changeableTransformer;
    }

    protected void pageable(Pageable pageable) {
        this.pageable = pageable;
    }

    protected T one() {
        if (beanTransformer != null) {
            query.unwrap(org.hibernate.Query.class).setResultTransformer(beanTransformer);
        }
        return (T) this.object();
    }

    protected List list() {
        if (beanTransformer != null) {
            query.unwrap(org.hibernate.Query.class).setResultTransformer(beanTransformer);
        }
        this.handlePageable();
        return (List) query.getResultList();
    }

    protected List> humpMapList() {
        query.unwrap(org.hibernate.Query.class).setResultTransformer(mapTransformer);
        this.handlePageable();
        return (List>) query.getResultList();
    }

    protected Map humpMap() {
        query.unwrap(org.hibernate.Query.class).setResultTransformer(mapTransformer);
        return (Map) this.object();
    }

    protected List> mapList() {
        query.unwrap(org.hibernate.Query.class).setResultTransformer(
                Transformers.ALIAS_TO_ENTITY_MAP);
        return (List>) query.getResultList();
    }

    protected Map map() {
        query.unwrap(org.hibernate.Query.class)
                .setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        return (Map) this.object();
    }

    protected List objects() {
        this.handlePageable();
        return (List) query.getResultList();
    }

    protected Object object() {
        try {
            return query.getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

    protected long total() {
        Object result = countQuery.getSingleResult();
        if (result instanceof Long) {
            return (long) result;
        }
        BigInteger bigint = (BigInteger) result;
        return bigint == null ? 0L : bigint.longValue();
    }

    protected void handlePageable() {
        if (this.pageable != null) {
            this.query.setFirstResult(Long.valueOf(this.pageable.getOffset()).intValue());
            this.query.setMaxResults(this.pageable.getPageSize());
        }
    }
}