Skip to content

Commit 44802c7

Browse files
committed
add UpdateSpecification.reassign()
1 parent b1705bc commit 44802c7

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

hibernate-core/src/main/java/org/hibernate/query/specification/UpdateSpecification.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.hibernate.query.restriction.Restriction;
1212
import 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.
@@ -36,18 +38,25 @@
3638
@Incubating
3739
public 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

hibernate-core/src/main/java/org/hibernate/query/specification/internal/MutationSpecificationImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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;

hibernate-core/src/main/java/org/hibernate/query/specification/internal/UpdateSpecificationImpl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.hibernate.query.specification.UpdateSpecification;
1212
import 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 );

hibernate-core/src/main/java/org/hibernate/query/sqm/tree/update/SqmSetClause.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.hibernate.query.sqm.tree.domain.SqmPath;
1616
import 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) {

0 commit comments

Comments
 (0)