
com.agapsys.jpa.CountBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jpa-utils Show documentation
Show all versions of jpa-utils Show documentation
Utility classes for JPA applications
The newest version!
/*
* Copyright 2015 Agapsys Tecnologia Ltda-ME.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.agapsys.jpa;
import com.agapsys.jpa.WhereClauseBuilder.QueryParameter;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Query;
public class CountBuilder extends AbstractFindBuilder {
public CountBuilder(Class entityClass) {
super(entityClass);
}
public CountBuilder(Class entityClass, String alias) {
super(entityClass, alias);
}
public CountBuilder(boolean distinct, Class entityClass) {
super(distinct, entityClass);
}
public CountBuilder(boolean distinct, Class entityClass, String alias) {
super(distinct, entityClass, alias);
}
@Override
public CountBuilder by(String field, FindOperator operator, Object... values) {
return (CountBuilder) super.by(field, operator, values);
}
@Override
public CountBuilder by(String field, Object... values) {
return (CountBuilder) super.by(field, values);
}
@Override
public CountBuilder by(String literal, QueryParameter...parameters) {
return (CountBuilder) super.by(literal, parameters);
}
@Override
public CountBuilder or(String field, FindOperator operator, Object... values) {
return (CountBuilder) super.or(field, operator, values);
}
@Override
public CountBuilder or(String field, Object... values) {
return (CountBuilder) super.or(field, values);
}
@Override
public CountBuilder or(String literal, QueryParameter...parameters) {
return (CountBuilder) super.or(literal, parameters);
}
@Override
public CountBuilder and(String field, FindOperator operator, Object... values) {
return (CountBuilder) super.and(field, operator, values);
}
@Override
public CountBuilder and(String field, Object... values) {
return (CountBuilder) super.and(field, values);
}
@Override
public CountBuilder and(String literal, QueryParameter...parameters) {
return (CountBuilder) super.and(literal, parameters);
}
@Override
public CountBuilder beginAndGroup() {
return (CountBuilder) super.beginAndGroup();
}
@Override
public CountBuilder beginOrGroup() {
return (CountBuilder) super.beginOrGroup();
}
@Override
public CountBuilder closeGroup() {
return (CountBuilder) super.closeGroup();
}
@Override
protected String getSelectClause() {
return "COUNT(1)";
}
public long count(EntityManager entityManager) {
Query query = prepareQuery(entityManager);
for (Map.Entry entry : getValues().entrySet()) {
query.setParameter(entry.getKey(), entry.getValue());
}
if (getOffset() != null)
query.setFirstResult(getOffset());
return (long) query.getSingleResult();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy