BrightSide Workbench Full Report + Source Code
JpaCriteriaUpdate.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.CriteriaUpdate;
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 import javax.persistence.metamodel.SingularAttribute;
34 
39 public class JpaCriteriaUpdate<E> {
40 
41  private final Class<E> persistentClass;
42  private final JpaCriteria jpaCriteria;
43  private CriteriaUpdate<E> criteriaUpdate;
44  protected Root<E> root;
45 
46  public JpaCriteriaUpdate(Class<E> persistentClass, JpaCriteria jpaCriteria) {
47  this.persistentClass = persistentClass;
48  this.jpaCriteria = jpaCriteria;
49  initCriteria();
50  }
51 
52  public CriteriaUpdate<E> update() {
53  return criteriaUpdate;
54  }
55 
56  /* Delegate CriteriaUpdate */
57 
58  public <Y, X extends Y> JpaCriteriaUpdate<E> set(SingularAttribute<? super E, Y> attribute, X value) {
59  criteriaUpdate.set(attribute, value);
60  return this;
61  }
62 
63  public <Y> JpaCriteriaUpdate<E> set(SingularAttribute<? super E, Y> attribute, Expression<? extends Y> value) {
64  criteriaUpdate.set(attribute, value);
65  return this;
66  }
67 
68  public <Y, X extends Y> JpaCriteriaUpdate<E> set(Path<Y> attribute, X value) {
69  criteriaUpdate.set(attribute, value);
70  return this;
71  }
72 
73  public <Y> JpaCriteriaUpdate<E> set(Path<Y> attribute, Expression<? extends Y> value) {
74  criteriaUpdate.set(attribute, value);
75  return this;
76  }
77 
78  public JpaCriteriaUpdate<E> set(String attributeName, Object value) {
79  criteriaUpdate.set(attributeName, value);
80  return this;
81  }
82 
83  public JpaCriteriaUpdate<E> where(Expression<Boolean> restriction) {
84  criteriaUpdate.where(restriction);
85  return this;
86  }
87 
88  public JpaCriteriaUpdate<E> where(Predicate... restrictions) {
89  criteriaUpdate.where(restrictions);
90  return this;
91  }
92 
93  public <U> JpaSubquery<U> subquery(Class<U> javaClass) {
94  return new JpaSubquery<>(javaClass, criteriaUpdate.subquery(javaClass));
95  }
96 
97  public Predicate getRestriction() {
98  return criteriaUpdate.getRestriction();
99  }
100 
101  /* Delegate Root */
102 
103  public Path field(String fieldName) {
104  return root.get(fieldName);
105  }
106 
107  public Selection<E> alias(String name) {
108  return root.alias(name);
109  }
110 
111  public <X, Y> Join<X, Y> join(String attributeName) {
112  return root.join(attributeName);
113  }
114 
115  public <X, Y> CollectionJoin<X, Y> joinCollection(String attributeName) {
116  return root.joinCollection(attributeName);
117  }
118 
119  public <X, Y> SetJoin<X, Y> joinSet(String attributeName) {
120  return root.joinSet(attributeName);
121  }
122 
123  public <X, Y> ListJoin<X, Y> joinList(String attributeName) {
124  return root.joinList(attributeName);
125  }
126 
127  public <X, K, V> MapJoin<X, K, V> joinMap(String attributeName) {
128  return root.joinMap(attributeName);
129  }
130 
131  public <X, Y> Join<X, Y> join(String attributeName, JoinType jt) {
132  return root.join(attributeName, jt);
133  }
134 
135  public <X, Y> CollectionJoin<X, Y> joinCollection(String attributeName, JoinType jt) {
136  return root.joinCollection(attributeName, jt);
137  }
138 
139  public <X, Y> SetJoin<X, Y> joinSet(String attributeName, JoinType jt) {
140  return root.joinSet(attributeName, jt);
141  }
142 
143  public <X, Y> ListJoin<X, Y> joinList(String attributeName, JoinType jt) {
144  return root.joinList(attributeName, jt);
145  }
146 
147  public <X, K, V> MapJoin<X, K, V> joinMap(String attributeName, JoinType jt) {
148  return root.joinMap(attributeName, jt);
149  }
150 
151  private void initCriteria() {
152  criteriaUpdate = jpaCriteria.builder().createCriteriaUpdate(persistentClass);
153  root = criteriaUpdate.from(persistentClass);
154  }
155 
156 }
JpaCriteriaUpdate(Class< E > persistentClass, JpaCriteria jpaCriteria)
JpaCriteriaUpdate< E > where(Predicate... restrictions)
JpaCriteriaUpdate< E > where(Expression< Boolean > restriction)