From 3f6090996736510c91a754589228140c209e2d4a Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Tue, 13 Feb 2024 11:13:23 +0100 Subject: [PATCH] HHH-17726 Add test for issue --- .../mapping/basic/TimeZoneMappingTests.java | 28 +++++++++++-- .../mapping/basic/ZoneOffsetMappingTests.java | 31 +++++++++++++-- .../mapping/type/java/ZoneMappingTests.java | 39 +++++++++++++------ 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/basic/TimeZoneMappingTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/basic/TimeZoneMappingTests.java index ab45c0a965..9067ee8e7d 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/basic/TimeZoneMappingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/basic/TimeZoneMappingTests.java @@ -8,9 +8,6 @@ package org.hibernate.orm.test.mapping.basic; import java.sql.Types; import java.util.TimeZone; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import jakarta.persistence.Table; import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping; @@ -19,10 +16,16 @@ import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -57,6 +60,25 @@ public class TimeZoneMappingTests { ); } + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-17726" ) + public void testUpdateQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> session.persist( new EntityWithTimeZone( 1, TimeZone.getDefault() ) ) ); + scope.inTransaction( session -> { + final TimeZone timeZone = TimeZone.getTimeZone( "UTC" ); + session.createMutationQuery( "update EntityWithTimeZone e set e.timeZone = :timeZone" ) + .setParameter( "timeZone", timeZone ) + .executeUpdate(); + final EntityWithTimeZone entity = session.find( EntityWithTimeZone.class, 1 ); + assertThat( entity.timeZone, equalTo( timeZone ) ); + } ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery( "delete EntityWithTimeZone" ).executeUpdate() ); + } + @Entity(name = "EntityWithTimeZone") @Table(name = "EntityWithTimeZone") public static class EntityWithTimeZone { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/basic/ZoneOffsetMappingTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/basic/ZoneOffsetMappingTests.java index 2f30c2fd4d..7667ebb739 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/basic/ZoneOffsetMappingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/basic/ZoneOffsetMappingTests.java @@ -8,9 +8,6 @@ package org.hibernate.orm.test.mapping.basic; import java.sql.Types; import java.time.ZoneOffset; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import jakarta.persistence.Table; import org.hibernate.metamodel.mapping.JdbcMapping; import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping; @@ -19,10 +16,16 @@ import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -57,6 +60,28 @@ public class ZoneOffsetMappingTests { ); } + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-17726" ) + public void testUpdateQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> session.persist( new EntityWithZoneOffset( + 1, + ZoneOffset.from( ZoneOffset.MIN ) + ) ) ); + scope.inTransaction( session -> { + final ZoneOffset zoneOffset = ZoneOffset.from( ZoneOffset.UTC ); + session.createMutationQuery( "update EntityWithZoneOffset e set e.zoneOffset = :zoneOffset" ) + .setParameter( "zoneOffset", zoneOffset ) + .executeUpdate(); + final EntityWithZoneOffset entity = session.find( EntityWithZoneOffset.class, 1 ); + assertThat( entity.zoneOffset, equalTo( zoneOffset ) ); + } ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery( "delete EntityWithZoneOffset" ).executeUpdate() ); + } + @Entity(name = "EntityWithZoneOffset") @Table(name = "EntityWithZoneOffset") public static class EntityWithZoneOffset { diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/ZoneMappingTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/ZoneMappingTests.java index 886c435215..94860eca83 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/ZoneMappingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/type/java/ZoneMappingTests.java @@ -18,10 +18,11 @@ import org.hibernate.persister.entity.EntityPersister; import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry; import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.Jira; import org.hibernate.testing.orm.junit.JiraKey; import org.hibernate.testing.orm.junit.SessionFactory; import org.hibernate.testing.orm.junit.SessionFactoryScope; - +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import jakarta.persistence.CollectionTable; @@ -70,18 +71,34 @@ public class ZoneMappingTests { session.persist( entity3 ); } ); - try { - scope.inTransaction( (session) -> { - session.createQuery( "from ZoneMappingTestEntity" ).list(); - }); - } - finally { - scope.inTransaction( (session) -> { - session.createQuery( "delete ZoneMappingTestEntity" ).executeUpdate(); - }); - } + scope.inTransaction( (session) -> session.createQuery( "from ZoneMappingTestEntity" ).list() ); } + @Test + @Jira( "https://hibernate.atlassian.net/browse/HHH-17726" ) + public void testUpdateQuery(SessionFactoryScope scope) { + scope.inTransaction( session -> session.persist( new ZoneMappingTestEntity( + 1, + "one", + ZoneId.systemDefault(), + ZoneOffset.MIN + ) ) ); + scope.inTransaction( session -> { + final ZoneId zoneId = ZoneId.of( "UTC" ); + final ZoneOffset zoneOffset = ZoneOffset.from( ZoneOffset.UTC ); + session.createMutationQuery( + "update ZoneMappingTestEntity e set e.zoneId = :zoneId, e.zoneOffset = :zoneOffset" + ).setParameter( "zoneId", zoneId ).setParameter( "zoneOffset", zoneOffset ).executeUpdate(); + final ZoneMappingTestEntity entity = session.find( ZoneMappingTestEntity.class, 1 ); + assertThat( entity.getZoneId() ).isEqualTo( zoneId ); + assertThat( entity.getZoneOffset() ).isEqualTo( zoneOffset ); + } ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( session -> session.createMutationQuery( "delete ZoneMappingTestEntity" ).executeUpdate() ); + } @Entity( name = "ZoneMappingTestEntity" ) @Table( name = "zone_map_test_entity" )