Skip to content

Commit 626bbd9

Browse files
author
tcordel
committed
HHH-19862: fix CriteriaUpdate for converted field
1 parent 44ee308 commit 626bbd9

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ public void setSetClause(SqmSetClause setClause) {
171171

172172
@Override
173173
public <Y, X extends Y> SqmUpdateStatement<T> set(SingularAttribute<? super T, Y> attribute, X value) {
174-
applyAssignment( getTarget().get( attribute ), (SqmExpression<? extends Y>) nodeBuilder().value( value ) );
174+
final SqmCriteriaNodeBuilder nodeBuilder = (SqmCriteriaNodeBuilder) nodeBuilder();
175+
SqmPath<Y> sqmAttribute = getTarget().get( attribute );
176+
applyAssignment( sqmAttribute, nodeBuilder.value( value, sqmAttribute) );
175177
return this;
176178
}
177179

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.jpa.criteria.convert;
6+
7+
import jakarta.persistence.criteria.CriteriaBuilder;
8+
import jakarta.persistence.criteria.CriteriaUpdate;
9+
import jakarta.persistence.criteria.Root;
10+
11+
import org.hibernate.testing.orm.junit.DomainModel;
12+
import org.hibernate.testing.orm.junit.SessionFactory;
13+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
14+
import org.junit.jupiter.api.Test;
15+
16+
import java.util.Date;
17+
18+
@DomainModel( annotatedClasses = { Log.class } )
19+
@SessionFactory
20+
public class ConvertedDateAttributeTest {
21+
@Test
22+
public void testConvertedFieldUpdateUsingStaticModel(SessionFactoryScope scope) {
23+
24+
scope.inTransaction( (session) -> {
25+
final CriteriaBuilder cb = session.getCriteriaBuilder();
26+
final CriteriaUpdate<Log> query = cb.createCriteriaUpdate( Log.class );
27+
query.set( Log_.date, new Date());
28+
session.createMutationQuery( query ).executeUpdate();
29+
} );
30+
31+
}
32+
33+
@Test
34+
public void testConvertedFieldUpdateUsingPath(SessionFactoryScope scope) {
35+
36+
scope.inTransaction( (session) -> {
37+
final CriteriaBuilder cb = session.getCriteriaBuilder();
38+
final CriteriaUpdate<Log> query = cb.createCriteriaUpdate( Log.class );
39+
final Root<Log> root = query.from( Log.class );
40+
query.set(root.get(Log_.date), new Date());
41+
session.createMutationQuery( query ).executeUpdate();
42+
} );
43+
44+
}
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.jpa.criteria.convert;
6+
7+
import jakarta.persistence.AttributeConverter;
8+
import jakarta.persistence.Convert;
9+
import jakarta.persistence.Converter;
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.Id;
12+
13+
import java.util.Date;
14+
@Entity
15+
public class Log {
16+
@Id
17+
private Long id;
18+
@Convert(converter = DateConverter.class)
19+
private Date date;
20+
@Converter
21+
static class DateConverter implements AttributeConverter<Date, Long> {
22+
@Override
23+
public Long convertToDatabaseColumn(Date date) {
24+
return date.getTime();
25+
}
26+
@Override
27+
public Date convertToEntityAttribute(Long dbDate) {
28+
return new Date( dbDate );
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)