File tree Expand file tree Collapse file tree 4 files changed +41
-6
lines changed
hibernate-core/src/main/java/org/hibernate/query Expand file tree Collapse file tree 4 files changed +41
-6
lines changed Original file line number Diff line number Diff line change 1111import org .hibernate .query .restriction .Restriction ;
1212import org .hibernate .query .specification .internal .UpdateSpecificationImpl ;
1313
14+ import java .util .List ;
15+
1416/**
1517 * Specialization of {@link MutationSpecification} for programmatic customization
1618 * of update queries.
3638@ Incubating
3739public interface UpdateSpecification <T > extends MutationSpecification <T > {
3840 /**
39- * If this {@code MutationSpecification} represents an {@code update}
40- * statement, add an assigment to a field or property of the target
41- * entity.
41+ * Add an assigment to a field or property of the target entity.
4242 *
4343 * @param assignment The assignment to add
4444 *
4545 * @return {@code this} for method chaining.
46- *
47- * @since 7.2
4846 */
4947 UpdateSpecification <T > assign (Assignment <? super T > assignment );
5048
49+ /**
50+ * Sets the assignments to fields or properties of the target entity.
51+ * If assignments were already specified, this method drops the previous
52+ * assignments in favor of the passed {@code assignments}.
53+ *
54+ * @param assignments The new assignments
55+ *
56+ * @return {@code this} for method chaining.
57+ */
58+ UpdateSpecification <T > reassign (List <? extends Assignment <? super T >> assignments );
59+
5160 @ Override
5261 UpdateSpecification <T > restrict (Restriction <? super T > restriction );
5362
Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ public enum MutationType {
5555 }
5656
5757 final List <BiConsumer <SqmDeleteOrUpdateStatement <T >, SqmRoot <T >>> specifications = new ArrayList <>();
58+
5859 private final String hql ;
5960 private final Class <T > mutationTarget ;
6061 private final SqmDeleteOrUpdateStatement <T > deleteOrUpdateStatement ;
Original file line number Diff line number Diff line change 1111import org .hibernate .query .specification .UpdateSpecification ;
1212import org .hibernate .query .sqm .tree .update .SqmUpdateStatement ;
1313
14+ import java .util .List ;
15+
1416/**
1517 * @author Gavin King
1618 */
@@ -39,6 +41,23 @@ public UpdateSpecification<T> assign(Assignment<? super T> assignment) {
3941 return this ;
4042 }
4143
44+ @ Override
45+ public UpdateSpecification <T > reassign (List <? extends Assignment <? super T >> assignments ) {
46+ specifications .add ( (sqmStatement , mutationTargetRoot ) -> {
47+ if ( sqmStatement instanceof SqmUpdateStatement <T > sqmUpdateStatement ) {
48+ final var setClause = sqmUpdateStatement .getSetClause ();
49+ if ( setClause != null ) {
50+ setClause .clearAssignments ();
51+ }
52+ assignments .forEach ( assignment -> assignment .apply ( sqmUpdateStatement ) );
53+ }
54+ else {
55+ throw new IllegalStateException ( "Delete query cannot perform assignment" );
56+ }
57+ } );
58+ return this ;
59+ }
60+
4261 @ Override
4362 public UpdateSpecification <T > restrict (Restriction <? super T > restriction ) {
4463 super .restrict ( restriction );
Original file line number Diff line number Diff line change 1515import org .hibernate .query .sqm .tree .domain .SqmPath ;
1616import org .hibernate .query .sqm .tree .expression .SqmExpression ;
1717
18+ import static java .util .Collections .unmodifiableList ;
19+
1820/**
1921 * @author Steve Ebersole
2022 */
@@ -38,7 +40,11 @@ public SqmSetClause copy(SqmCopyContext context) {
3840 }
3941
4042 public List <SqmAssignment <?>> getAssignments () {
41- return Collections .unmodifiableList ( assignments );
43+ return unmodifiableList ( assignments );
44+ }
45+
46+ public void clearAssignments () {
47+ assignments .clear ();
4248 }
4349
4450 public void addAssignment (SqmAssignment <?> assignment ) {
You can’t perform that action at this time.
0 commit comments