BrightSide Workbench Full Report + Source Code
JpaQuery.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.CriteriaQuery;
23 import javax.persistence.criteria.Order;
24 import javax.persistence.criteria.Path;
25 import javax.persistence.criteria.Selection;
26 
31 public class JpaQuery<E> extends AbstractJpaQuery<E> {
32 
33  private final JpaCriteria jpaCriteria;
34 
35  protected JpaQuery(Class<E> javaClass, JpaCriteria jpaCriteria) {
36  super(javaClass);
37  this.jpaCriteria = jpaCriteria;
38  initQuery();
39  }
40 
41  @Override
42  public JpaQuery<E> select() {
43  ((CriteriaQuery<E>) query).select(root().root());
44  return this;
45  }
46 
47  @Override
48  public JpaQuery<E> select(String... columns) {
49  Path selection = null;
50  for(int i = 0; i < columns.length; i++) {
51  if(i == 0) {
52  selection = root().get(columns[i]);
53  } else if(selection != null) {
54  selection = selection.get(columns[i]);
55  }
56  }
57  ((CriteriaQuery<E>) query).select(selection);
58  return this;
59  }
60 
61  public JpaQuery<E> select(Selection<? extends E> selection) {
62  ((CriteriaQuery<E>) query).select(selection);
63  return this;
64  }
65 
66  public JpaQuery<E> multiselect(Selection<?>... selections) {
67  ((CriteriaQuery<E>) query).multiselect(selections);
68  return this;
69  }
70 
71  public JpaQuery<E> multiselect(List<Selection<?>> selectionList) {
72  ((CriteriaQuery<E>) query).multiselect(selectionList);
73  return this;
74  }
75 
76  public JpaQuery<E> orderBy(Order... o) {
77  ((CriteriaQuery<E>) query).orderBy(o);
78  return this;
79  }
80 
81  public JpaQuery<E> orderBy(List o) {
82  ((CriteriaQuery<E>) query).orderBy(o);
83  return this;
84  }
85 
86  public <T> JpaSubquery<T> subquery(Class<T> javaClass) {
87  return new JpaSubquery<>(javaClass, query.subquery(persistentClass));
88  }
89 
90  private void initQuery() {
91  query = jpaCriteria.builder().createQuery(persistentClass);
92  }
93 
94 }
JpaQuery< E > select(Selection<? extends E > selection)
Definition: JpaQuery.java:61
JpaQuery< E > orderBy(Order... o)
Definition: JpaQuery.java:76
JpaQuery(Class< E > javaClass, JpaCriteria jpaCriteria)
Definition: JpaQuery.java:35
JpaQuery< E > orderBy(List o)
Definition: JpaQuery.java:81
JpaQuery< E > select()
Definition: JpaQuery.java:42
JpaQuery< E > multiselect(Selection<?>... selections)
Definition: JpaQuery.java:66
JpaQuery< E > select(String... columns)
Definition: JpaQuery.java:48
JpaQuery< E > multiselect(List< Selection<?>> selectionList)
Definition: JpaQuery.java:71