Search code examples
hibernatejpacriteriaentitymanagerhibernate-entitymanager

entityMananger not working with Date parameter


Using EntityManager approach with Hibernate. Whenever using a date field query I am expecting it to look for date with complete date and time, but it only uses date not time.

 where cou0_.expiry_date>'22-Jan-16'

spring config

<bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_caches}</prop>
                <prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
                <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
                <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
            </props>
        </property>
    </bean>

Props

hibernate.hbm2ddl.auto = update
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
hibernate.c3p0.min_size = 5
hibernate.c3p0.max_size = 10
hibernate.c3p0.timeout = 300
hibernate.c3p0.max_statements = 50
hibernate.c3p0.idle_test_period = 3000
hibernate.generate_statistics = true
hibernate.show_sql=true
hibernate.use_sql_comments=true

Column declared as

@Temporal(TemporalType.TIMESTAMP)
    @Column(name = "expiry_date")
    private Date expiryDate;

Query Code

Query query = getEntityManager().createQuery("from **** where c.expiryDate  > :expiryDate");
        Date now = new Date();
        now.setHours(8);
        System.out.println("Date--"+now);
        query.setParameter("expiryDate", now,TemporalType.TIMESTAMP);
        List<****> entity = query.getResultList();

Solution

  • Issue was not with entity manager. It was with spy that I was using to trace the query. Spy have configurable property to show dates in format. I didn't configured it so it want using default format i.e. dd-MMM-yyyy