BrightSide Workbench Full Report + Source Code
JpaCriteriaDelete.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 javax.persistence.criteria.CollectionJoin;
22 import javax.persistence.criteria.CriteriaDelete;
23 import javax.persistence.criteria.Expression;
24 import javax.persistence.criteria.Join;
25 import javax.persistence.criteria.JoinType;
26 import javax.persistence.criteria.ListJoin;
27 import javax.persistence.criteria.MapJoin;
28 import javax.persistence.criteria.Path;
29 import javax.persistence.criteria.Predicate;
30 import javax.persistence.criteria.Root;
31 import javax.persistence.criteria.Selection;
32 import javax.persistence.criteria.SetJoin;
33 
38 public class JpaCriteriaDelete<E> {
39 
40  private final Class<E> persistentClass;
41  private final JpaCriteria jpaCriteria;
42  private CriteriaDelete<E> criteriaDelete;
43  protected Root<E> root;
44 
45  public JpaCriteriaDelete(Class<E> persistentClass, JpaCriteria jpaCriteria) {
46  this.persistentClass = persistentClass;
47  this.jpaCriteria = jpaCriteria;
48  initCriteria();
49  }
50 
51  public CriteriaDelete<E> delete() {
52  return criteriaDelete;
53  }
54 
55  /* Delegate CriteriaDelete */
56 
57  public JpaCriteriaDelete<E> where(Expression<Boolean> restriction) {
58  criteriaDelete.where(restriction);
59  return this;
60  }
61 
62  public JpaCriteriaDelete<E> where(Predicate... restrictions) {
63  criteriaDelete.where(restrictions);
64  return this;
65  }
66 
67  public <U> JpaSubquery<U> subquery(Class<U> javaClass) {
68  return new JpaSubquery<>(javaClass, criteriaDelete.subquery(javaClass));
69  }
70 
71  public Predicate getRestriction() {
72  return criteriaDelete.getRestriction();
73  }
74 
75  /* Delegate Root */
76 
77  public Path field(String fieldName) {
78  return root.get(fieldName);
79  }
80 
81  public Selection<E> alias(String name) {
82  return root.alias(name);
83  }
84 
85  public <X, Y> Join<X, Y> join(String attributeName) {
86  return root.join(attributeName);
87  }
88 
89  public <X, Y> CollectionJoin<X, Y> joinCollection(String attributeName) {
90  return root.joinCollection(attributeName);
91  }
92 
93  public <X, Y> SetJoin<X, Y> joinSet(String attributeName) {
94  return root.joinSet(attributeName);
95  }
96 
97  public <X, Y> ListJoin<X, Y> joinList(String attributeName) {
98  return root.joinList(attributeName);
99  }
100 
101  public <X, K, V> MapJoin<X, K, V> joinMap(String attributeName) {
102  return root.joinMap(attributeName);
103  }
104 
105  public <X, Y> Join<X, Y> join(String attributeName, JoinType jt) {
106  return root.join(attributeName, jt);
107  }
108 
109  public <X, Y> CollectionJoin<X, Y> joinCollection(String attributeName, JoinType jt) {
110  return root.joinCollection(attributeName, jt);
111  }
112 
113  public <X, Y> SetJoin<X, Y> joinSet(String attributeName, JoinType jt) {
114  return root.joinSet(attributeName, jt);
115  }
116 
117  public <X, Y> ListJoin<X, Y> joinList(String attributeName, JoinType jt) {
118  return root.joinList(attributeName, jt);
119  }
120 
121  public <X, K, V> MapJoin<X, K, V> joinMap(String attributeName, JoinType jt) {
122  return root.joinMap(attributeName, jt);
123  }
124 
125  private void initCriteria() {
126  criteriaDelete = jpaCriteria.builder().createCriteriaDelete(persistentClass);
127  root = criteriaDelete.from(persistentClass);
128  }
129 
130 }
JpaCriteriaDelete(Class< E > persistentClass, JpaCriteria jpaCriteria)
JpaCriteriaDelete< E > where(Expression< Boolean > restriction)
JpaCriteriaDelete< E > where(Predicate... restrictions)