Support date literals in HQL

Description

There is no value handler for date types defined in the ValueHandlerFactory class. This means that attempting to select a date literal will result in a null pointer exception.

I ran into this bug when attempting to implement coalesce using date properties. The following code throws an exception:

query.select(coalesce(pathToDatePropertyOnEntity, defaultDateLiteral));

Coalesce should select the date property off some entity unless it is null, in which case it will select the supplied date literal. But because no handling for date literals exists, this instruction throws a null pointer exception.

Simple test case:

@Test
public void testFoo() throws Exception
{
//insert an element into the database
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
SampleClass sample = new SampleClass();
session.save(sample);
session.getTransaction().commit();

criteriaQuery.from(SampleClass.class);
criteriaQuery.select(criteriaBuilder.literal(new Date()));

TypedQuery<Date> jpaQuery = entityManager.createQuery(criteriaQuery);

List<Date> results = jpaQuery.getResultList();
Assert.assertEquals(1, results.size());
}

Test Output:

DateLiteralJpaEvaluatorTest,testFoo
Oct 29, 2013 10:37:56 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Oct 29, 2013 10:37:56 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
Oct 29, 2013 10:37:56 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 29, 2013 10:37:56 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Oct 29, 2013 10:37:57 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.HSQLDialect
Oct 29, 2013 10:37:57 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
Oct 29, 2013 10:37:57 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Oct 29, 2013 10:37:57 AM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.0.0.Final
Oct 29, 2013 10:37:58 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Oct 29, 2013 10:37:58 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
Oct 29, 2013 10:37:58 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
Oct 29, 2013 10:37:58 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: SampleClass
Oct 29, 2013 10:37:58 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: SampleClass
Oct 29, 2013 10:37:58 AM org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata
INFO: HHH000262: Table not found: SampleClass
Oct 29, 2013 10:37:58 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
create table SampleClass (id binary(255) not null, booleanProperty boolean, dateProperty timestamp, numberProperty numeric(19,2), stringProperty varchar(255), primary key (id))
Oct 29, 2013 10:37:58 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Oct 29, 2013 10:37:58 AM org.hibernate.type.TypeFactory$TypeScopeImpl injectSessionFactory
WARN: HHH000233: Scoping types to session factory org.hibernate.internal.SessionFactoryImpl@7a8eb36a after already scoped org.hibernate.internal.SessionFactoryImpl@5dd23619

java.lang.NullPointerException
at org.hibernate.ejb.criteria.expression.LiteralExpression.renderProjection(LiteralExpression.java:82)
at org.hibernate.ejb.criteria.QueryStructure.render(QueryStructure.java:250)
at org.hibernate.ejb.criteria.CriteriaQueryImpl.render(CriteriaQueryImpl.java:338)
at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:223)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:622)
at com.tr.grc.storage.eval.jpa.date.DateLiteralJpaEvaluatorTest.testFoo(DateLiteralJpaEvaluatorTest.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Environment

hibernate-core-4.2.5.Final, hsqldb 2.3.0

Assignee

Unassigned

Reporter

Mike Wilklow

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

Yes, likely

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

Priority

Minor
Configure