Fix LocalTime and OffsetTime parameter binding

This commit is contained in:
Andrea Boriero 2022-02-09 18:09:49 +01:00 committed by Andrea Boriero
parent 83306d588d
commit d8a98af76e
3 changed files with 133 additions and 1 deletions

View File

@ -226,7 +226,7 @@ ext {
'jdbc.driver': 'com.microsoft.sqlserver.jdbc.SQLServerDriver',
'jdbc.user' : 'sa',
'jdbc.pass' : 'Hibernate_orm_test',
'jdbc.url' : 'jdbc:sqlserver://' + dbHost + ';databaseName=hibernate_orm_test',
'jdbc.url' : 'jdbc:sqlserver://' + dbHost + ';databaseName=hibernate_orm_test;sendTimeAsDatetime=false',
'connection.init_sql' : ''
],
informix : [

View File

@ -7,6 +7,7 @@
package org.hibernate.query.internal;
import java.time.Instant;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
@ -164,6 +165,14 @@ public class BindingTypeHelper {
return typeConfiguration.getBasicTypeRegistry().resolve( StandardBasicTypes.TIME );
}
if ( LocalTime.class.isAssignableFrom( javaType ) ) {
return typeConfiguration.getBasicTypeRegistry().resolve( StandardBasicTypes.LOCAL_TIME );
}
if ( OffsetTime.class.isAssignableFrom( javaType ) ) {
return typeConfiguration.getBasicTypeRegistry().resolve( StandardBasicTypes.OFFSET_TIME );
}
throw new IllegalArgumentException( "Unsure how to handle given Java type [" + javaType.getName() + "] as TemporalType#TIME" );
}
}

View File

@ -0,0 +1,123 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.orm.test.jpa.compliance;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.util.List;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.TypedQuery;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Jpa(
annotatedClasses = LocalTimeTest.TestEntity.class
)
public class LocalTimeTest {
private static final LocalTime LOCAL_TIME = LocalTime.now();
private static final OffsetTime OFFSET_TIME = OffsetTime.of( LOCAL_TIME, ZoneOffset.ofHours( 2 ) );
@BeforeEach
public void setUp(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
TestEntity d1 = new TestEntity(
1L,
LOCAL_TIME,
OffsetTime.of( LocalTime.of( 12, 18, 21 ), ZoneOffset.ofHours( 3 ) )
);
TestEntity d2 = new TestEntity(
2L,
LocalTime.of( 12, 18, 21 ),
OFFSET_TIME
);
entityManager.persist( d1 );
entityManager.persist( d2 );
}
);
}
@AfterEach
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager ->
entityManager.createQuery( "delete from TestEntity" ).executeUpdate()
);
}
@Test
public void testLocalTimeParameterQuery(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
TypedQuery<TestEntity> query = entityManager.createQuery(
"select t from TestEntity t where t.time = :time",
TestEntity.class
);
query.setParameter( "time", LOCAL_TIME );
List<TestEntity> result = query.getResultList();
assertEquals( 1, result.size() );
assertEquals( 1, result.get( 0 ).getId() );
}
);
}
@Test
public void testOffsetTimeParameterQuery(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
TypedQuery<TestEntity> query = entityManager.createQuery(
"select t from TestEntity t where t.offset = :offset",
TestEntity.class
);
query.setParameter( "offset", OFFSET_TIME );
List<TestEntity> result = query.getResultList();
assertEquals( 1, result.size() );
assertEquals( 2, result.get( 0 ).getId() );
}
);
}
@Entity(name = "TestEntity")
public static class TestEntity {
@Id
private Long id;
@Column(name = "TIME_ATTRIBUTE")
private LocalTime time;
@Column(name = "OFFSET_ATTRIBUTE")
private OffsetTime offset;
public TestEntity() {
}
public TestEntity(Long id, LocalTime time, OffsetTime offset) {
this.id = id;
this.time = time;
this.offset = offset;
}
public Long getId() {
return id;
}
}
}