cast integers to Durations

This commit is contained in:
Gavin 2023-04-09 16:41:27 +02:00 committed by Gavin King
parent 511399c152
commit 77b96a6b4b
2 changed files with 9 additions and 0 deletions

View File

@ -339,6 +339,7 @@ public class TypeConfiguration implements SessionFactoryObserver, Serializable {
case "zoneddatetime": return getBasicTypeForJavaType( ZonedDateTime.class );
case "biginteger": return getBasicTypeForJavaType( BigInteger.class );
case "bigdecimal": return getBasicTypeForJavaType( BigDecimal.class );
case "duration": return getBasicTypeForJavaType( Duration.class );
case "binary": return getBasicTypeForJavaType( byte[].class );
//this one is very fragile ... works well for BIT or BOOLEAN columns only
//works OK, I suppose, for integer columns, but not at all for char columns

View File

@ -1256,6 +1256,14 @@ public class FunctionTests {
assertEquals( 5*60*1_000_000_000L,
session.createQuery("select cast(5 minute as Long)", Long.class)
.getSingleResult() );
assertEquals( Duration.of(5, ChronoUnit.MINUTES),
session.createQuery("select cast(?1 as Duration)", Duration.class)
.setParameter(1, 5*60*1000000000L)
.getSingleResult() );
assertEquals( Duration.of(1, ChronoUnit.DAYS),
session.createQuery("select cast(?1 as Duration)", Duration.class)
.setParameter(1, 24*60*60*1000000000L)
.getSingleResult() );
}
);
}