Hibernate picking incorrect TypeDescriptor

609 views Asked by At

I am using Hibernate 5 with JPA. Below is some code snippets. The Object is built using JAXB and I prefer not to change the response type from Calendar as it will impact other applications as well.

class Object {
    @XmlSchemaType(
        name = "dateTime"
    )
    @Temporal(TemporalType.TIMESTAMP)
    protected Calendar startDate;
}

Query executed is

select count(m) from Message as m where start_date > :startDate

Parameter is set as follows

query.setParameter("startDate", object.getStartDate(), TemporalType.TIMESTAMP);

However, when executing the code which invokes the query, I get the following stack trace.

Caused by: java.lang.ClassCastException: java.util.GregorianCalendar cannot be cast to java.util.Date
    at org.hibernate.type.descriptor.java.JdbcTimestampTypeDescriptor.unwrap(JdbcTimestampTypeDescriptor.java:24)
    at org.hibernate.type.descriptor.sql.TimestampTypeDescriptor$1.doBind(TimestampTypeDescriptor.java:48)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:73)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:276)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:271)
    at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:53)
    at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:648)
    at org.hibernate.loader.Loader.bindPreparedStatement(Loader.java:2113)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2090)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2022)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2000)
    at org.hibernate.loader.Loader.doQuery(Loader.java:951)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:352)
    at org.hibernate.loader.Loader.doList(Loader.java:2831)
    at org.hibernate.loader.Loader.doList(Loader.java:2813)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2645)
    at org.hibernate.loader.Loader.list(Loader.java:2640)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:400)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1412)
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1565)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533)
    at org.hibernate.query.Query.getResultList(Query.java:165)

Note It chooses the JdbcTimestampTypeDescriptor which is hard coded for the object type Date which is potentially where my issue lies.

Question

  • What am I doing wrong?
  • How can I influence hibernate to choose the CalendarTypeDescriptor? Would that be the right approach if so?

UPDATE I do not want to have to change the code as this similar code is in multiple places/repositories and I dont want to update all of them

1

There are 1 answers

1
Guillaume On

You can convert your calendar object into a date with the getTime() method:

query.setParameter("startDate", object.getStartDate().getTime(), TemporalType.TIMESTAMP);