BrightSide Workbench Full Report + Source Code
AbstractJpaQuery.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.jpa.query;
20 
21 import java.util.List;
22 import javax.persistence.criteria.AbstractQuery;
23 import javax.persistence.criteria.CriteriaQuery;
24 import javax.persistence.criteria.Expression;
25 import javax.persistence.criteria.Predicate;
26 
31 public abstract class AbstractJpaQuery<E> {
32 
33  protected final Class<E> persistentClass;
34  protected AbstractQuery<E> query;
35  protected JpaRoot root;
36 
37  public AbstractJpaQuery(Class<E> persistentClass) {
38  this.persistentClass = persistentClass;
39  }
40 
41  public JpaRoot<E> root() {
42  if(root == null) {
43  root = new JpaRoot<>(query.from(persistentClass));
44  }
45  return root;
46  }
47 
48  public <T> JpaRoot<T> root(Class<T> javaClass) {
49  return new JpaRoot<>(query.from(javaClass));
50  }
51 
52  public CriteriaQuery<E> query() {
53  return (CriteriaQuery<E>) query;
54  }
55 
56  public abstract AbstractJpaQuery<E> select();
57  public abstract AbstractJpaQuery<E> select(String... columns);
58 
59  public AbstractJpaQuery<E> where(Expression<Boolean> restriction) {
60  query.where(restriction);
61  return this;
62  }
63 
64  public AbstractJpaQuery<E> where(Predicate... restrictions) {
65  query.where(restrictions);
66  return this;
67  }
68 
69  public AbstractJpaQuery<E> groupBy(Expression<?>... grouping) {
70  query.groupBy(grouping);
71  return this;
72  }
73 
74  public AbstractJpaQuery<E> groupBy(List<Expression<?>> grouping) {
75  query.groupBy(grouping);
76  return this;
77  }
78 
79  public AbstractJpaQuery<E> having(Expression<Boolean> restriction) {
80  query.having(restriction);
81  return this;
82  }
83 
84  public AbstractJpaQuery<E> having(Predicate... restrictions) {
85  query.having(restrictions);
86  return this;
87  }
88 
90  query.distinct(distinct);
91  return this;
92  }
93 
94 }
AbstractJpaQuery< E > where(Expression< Boolean > restriction)
AbstractJpaQuery< E > having(Expression< Boolean > restriction)
abstract AbstractJpaQuery< E > select(String... columns)
abstract AbstractJpaQuery< E > select()
AbstractJpaQuery< E > having(Predicate... restrictions)
AbstractJpaQuery< E > groupBy(Expression<?>... grouping)
AbstractJpaQuery< E > distinct(boolean distinct)
AbstractJpaQuery< E > where(Predicate... restrictions)
AbstractJpaQuery(Class< E > persistentClass)
AbstractJpaQuery< E > groupBy(List< Expression<?>> grouping)