|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.query.hql.instantiation; |
| 6 | + |
| 7 | +import org.hibernate.testing.orm.domain.StandardDomainModel; |
| 8 | +import org.hibernate.testing.orm.domain.gambit.EntityOfBasics; |
| 9 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 10 | +import org.hibernate.testing.orm.junit.Jira; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 13 | +import org.junit.jupiter.api.AfterAll; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import java.sql.Date; |
| 18 | +import java.sql.Time; |
| 19 | +import java.sql.Timestamp; |
| 20 | +import java.time.LocalDate; |
| 21 | +import java.time.LocalDateTime; |
| 22 | +import java.time.LocalTime; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | + |
| 26 | +@DomainModel(standardModels = StandardDomainModel.GAMBIT) |
| 27 | +@SessionFactory |
| 28 | +@Jira("https://hibernate.atlassian.net/browse/HHH-4179") |
| 29 | +public class DynamicInstantiationTemporalTest { |
| 30 | + @BeforeAll |
| 31 | + public void prepareData(final SessionFactoryScope scope) { |
| 32 | + scope.inTransaction( session -> { |
| 33 | + EntityOfBasics entityOfBasics = new EntityOfBasics( 1 ); |
| 34 | + entityOfBasics.setTheLocalDate( LocalDate.EPOCH ); |
| 35 | + entityOfBasics.setTheLocalDateTime( LocalDate.EPOCH.atStartOfDay() ); |
| 36 | + entityOfBasics.setTheLocalTime( LocalTime.MIN ); |
| 37 | + entityOfBasics.setTheDate( Date.valueOf( entityOfBasics.getTheLocalDate() ) ); |
| 38 | + entityOfBasics.setTheTimestamp( Timestamp.valueOf( entityOfBasics.getTheLocalDateTime() ) ); |
| 39 | + entityOfBasics.setTheTime( Time.valueOf( entityOfBasics.getTheLocalTime() ) ); |
| 40 | + session.persist( entityOfBasics ); |
| 41 | + } ); |
| 42 | + } |
| 43 | + |
| 44 | + @AfterAll |
| 45 | + public void cleanUpData(final SessionFactoryScope scope) { |
| 46 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testJavaTime(final SessionFactoryScope scope) { |
| 51 | + scope.inSession( session -> { |
| 52 | + final var result = session.createQuery( |
| 53 | + "select new " + JavaTimeDto.class.getName() + "(e.theLocalDate, e.theLocalTime, e.theLocalDateTime) from EntityOfBasics e", |
| 54 | + JavaTimeDto.class |
| 55 | + ).getSingleResult(); |
| 56 | + EntityOfBasics entityOfBasics = session.find( EntityOfBasics.class, 1 ); |
| 57 | + assertEquals( entityOfBasics.getTheLocalDate(), result.getTheLocalDate() ); |
| 58 | + assertEquals( entityOfBasics.getTheLocalTime(), result.getTheLocalTime() ); |
| 59 | + assertEquals( entityOfBasics.getTheLocalDateTime(), result.getTheLocalDateTime() ); |
| 60 | + } ); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testJavaSql(final SessionFactoryScope scope) { |
| 65 | + scope.inSession( session -> { |
| 66 | + final var result = session.createQuery( |
| 67 | + "select new " + JavaSqlDto.class.getName() + "(e.theDate, e.theTime, e.theTimestamp) from EntityOfBasics e", |
| 68 | + JavaSqlDto.class |
| 69 | + ).getSingleResult(); |
| 70 | + EntityOfBasics entityOfBasics = session.find( EntityOfBasics.class, 1 ); |
| 71 | + assertEquals( entityOfBasics.getTheDate(), result.getTheDate() ); |
| 72 | + assertEquals( entityOfBasics.getTheTime(), result.getTheTime() ); |
| 73 | + assertEquals( entityOfBasics.getTheTimestamp(), result.getTheTimestamp() ); |
| 74 | + } ); |
| 75 | + } |
| 76 | + |
| 77 | + public static class JavaSqlDto { |
| 78 | + private java.sql.Date theDate; |
| 79 | + private java.sql.Time theTime; |
| 80 | + private java.sql.Timestamp theTimestamp; |
| 81 | + |
| 82 | + public JavaSqlDto(Date theDate, Time theTime, Timestamp theTimestamp) { |
| 83 | + this.theDate = theDate; |
| 84 | + this.theTime = theTime; |
| 85 | + this.theTimestamp = theTimestamp; |
| 86 | + } |
| 87 | + |
| 88 | + public Date getTheDate() { |
| 89 | + return theDate; |
| 90 | + } |
| 91 | + |
| 92 | + public Time getTheTime() { |
| 93 | + return theTime; |
| 94 | + } |
| 95 | + |
| 96 | + public Timestamp getTheTimestamp() { |
| 97 | + return theTimestamp; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static class JavaTimeDto { |
| 102 | + private LocalDate theLocalDate; |
| 103 | + private LocalTime theLocalTime; |
| 104 | + private LocalDateTime theLocalDateTime; |
| 105 | + |
| 106 | + public JavaTimeDto(LocalDate theLocalDate, LocalTime theLocalTime, LocalDateTime theLocalDateTime) { |
| 107 | + this.theLocalDate = theLocalDate; |
| 108 | + this.theLocalTime = theLocalTime; |
| 109 | + this.theLocalDateTime = theLocalDateTime; |
| 110 | + } |
| 111 | + |
| 112 | + public LocalDate getTheLocalDate() { |
| 113 | + return theLocalDate; |
| 114 | + } |
| 115 | + |
| 116 | + public LocalTime getTheLocalTime() { |
| 117 | + return theLocalTime; |
| 118 | + } |
| 119 | + |
| 120 | + public LocalDateTime getTheLocalDateTime() { |
| 121 | + return theLocalDateTime; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments