From 1f0b4a35ba80aac97177c2cff0422ea083513367 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Thu, 2 Jan 2025 15:39:00 +0100 Subject: [PATCH] HHH-4179 Test constructor syntax with temporal types --- .../DynamicInstantiationTemporalTest.java | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/instantiation/DynamicInstantiationTemporalTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/instantiation/DynamicInstantiationTemporalTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/instantiation/DynamicInstantiationTemporalTest.java new file mode 100644 index 000000000000..42c7e61efb73 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/instantiation/DynamicInstantiationTemporalTest.java @@ -0,0 +1,124 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.orm.test.query.hql.instantiation; + +import org.hibernate.testing.orm.domain.StandardDomainModel; +import org.hibernate.testing.orm.domain.gambit.EntityOfBasics; +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.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.sql.Date; +import java.sql.Time; +import java.sql.Timestamp; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@DomainModel(standardModels = StandardDomainModel.GAMBIT) +@SessionFactory +@Jira("https://hibernate.atlassian.net/browse/HHH-4179") +public class DynamicInstantiationTemporalTest { + @BeforeAll + public void prepareData(final SessionFactoryScope scope) { + scope.inTransaction( session -> { + EntityOfBasics entityOfBasics = new EntityOfBasics( 1 ); + entityOfBasics.setTheLocalDate( LocalDate.EPOCH ); + entityOfBasics.setTheLocalDateTime( LocalDate.EPOCH.atStartOfDay() ); + entityOfBasics.setTheLocalTime( LocalTime.MIN ); + entityOfBasics.setTheDate( Date.valueOf( entityOfBasics.getTheLocalDate() ) ); + entityOfBasics.setTheTimestamp( Timestamp.valueOf( entityOfBasics.getTheLocalDateTime() ) ); + entityOfBasics.setTheTime( Time.valueOf( entityOfBasics.getTheLocalTime() ) ); + session.persist( entityOfBasics ); + } ); + } + + @AfterAll + public void cleanUpData(final SessionFactoryScope scope) { + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); + } + + @Test + void testJavaTime(final SessionFactoryScope scope) { + scope.inSession( session -> { + final var result = session.createQuery( + "select new " + JavaTimeDto.class.getName() + "(e.theLocalDate, e.theLocalTime, e.theLocalDateTime) from EntityOfBasics e", + JavaTimeDto.class + ).getSingleResult(); + EntityOfBasics entityOfBasics = session.find( EntityOfBasics.class, 1 ); + assertEquals( entityOfBasics.getTheLocalDate(), result.getTheLocalDate() ); + assertEquals( entityOfBasics.getTheLocalTime(), result.getTheLocalTime() ); + assertEquals( entityOfBasics.getTheLocalDateTime(), result.getTheLocalDateTime() ); + } ); + } + + @Test + void testJavaSql(final SessionFactoryScope scope) { + scope.inSession( session -> { + final var result = session.createQuery( + "select new " + JavaSqlDto.class.getName() + "(e.theDate, e.theTime, e.theTimestamp) from EntityOfBasics e", + JavaSqlDto.class + ).getSingleResult(); + EntityOfBasics entityOfBasics = session.find( EntityOfBasics.class, 1 ); + assertEquals( entityOfBasics.getTheDate(), result.getTheDate() ); + assertEquals( entityOfBasics.getTheTime(), result.getTheTime() ); + assertEquals( entityOfBasics.getTheTimestamp(), result.getTheTimestamp() ); + } ); + } + + public static class JavaSqlDto { + private java.sql.Date theDate; + private java.sql.Time theTime; + private java.sql.Timestamp theTimestamp; + + public JavaSqlDto(Date theDate, Time theTime, Timestamp theTimestamp) { + this.theDate = theDate; + this.theTime = theTime; + this.theTimestamp = theTimestamp; + } + + public Date getTheDate() { + return theDate; + } + + public Time getTheTime() { + return theTime; + } + + public Timestamp getTheTimestamp() { + return theTimestamp; + } + } + + public static class JavaTimeDto { + private LocalDate theLocalDate; + private LocalTime theLocalTime; + private LocalDateTime theLocalDateTime; + + public JavaTimeDto(LocalDate theLocalDate, LocalTime theLocalTime, LocalDateTime theLocalDateTime) { + this.theLocalDate = theLocalDate; + this.theLocalTime = theLocalTime; + this.theLocalDateTime = theLocalDateTime; + } + + public LocalDate getTheLocalDate() { + return theLocalDate; + } + + public LocalTime getTheLocalTime() { + return theLocalTime; + } + + public LocalDateTime getTheLocalDateTime() { + return theLocalDateTime; + } + } +}