Hibernate and Oracle DATE column using HQL query

conorgriffin

I have an Oracle database with a DATE column. A sample value from this column is 15/12/2014 15:20:15 in the format DD/MM/YYYY HH:MM:SS.

I have this column defined as follows in my java Entity class:

@Column(name = "SVC_EVENT_END_DTTM", columnDefinition = "timestamp")
private Date dateTime;

The following HQL query works:

public List<ServiceEventLogEntity> getServiceEventLogEntries(DateTime from,
        DateTime to) {
    Query query = getSession().createQuery("FROM ServiceEventLogEntity e "
            + "WHERE e.dateTime between :start and :end "
            + "ORDER BY e.dateTime");
    query.setDate("start", from.toDate());
    query.setDate("end", to.toDate());
    query.setMaxResults(100);
    if(to.isBefore(new DateTime())) {
        // Only caching results for queries in the past.
        // Otherwise we could cache incomplete results when 
        // the end of the query range is in the future
        query.setCacheable(true);
    }
    try {
        return (List<ServiceEventLogEntity>) query.list();
    } catch (HibernateException hex) {
        hex.printStackTrace();
    }
    return null;
}

However, this only seems to work when the start and end times span a day transition, see the trace messages below:

[http-8080-1] TRACE org.hibernate.type.descriptor.sql.BasicBinder  - binding parameter [1] as [DATE] - Sat Dec 13 23:35:00 GMT 2014
[http-8080-1] TRACE org.hibernate.type.descriptor.sql.BasicBinder  - binding parameter [2] as [DATE] - Sun Dec 14 00:35:00 GMT 2014

I'm guessing that this is due to the fact that my DATE objects only represent a day and don't have a time element. I want to be able to query on minutes/hours not days. I've tried various approaches such as using setTimestamp instead of setDate but this just ends up resulting in the query never coming back with results, even in a much narrower window of time (5 minutes).

How can I modify either my entity or my query so I can go to a more granular level to enable me to query by minute or hour instead of day?

przemek hertel

Try to use:

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "SVC_EVENT_END_DTTM")
private Date dateTime;

Some javadoc from Temporal source code says:

[...] This annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar [...]

If you don't need milliseconds, than you can use Oracle's DATE type in column (which is up to seconds). When you need millis than you must use Oracle's TIMESTAMP type.

== EDIT ==

Additionaly you should use query.setTimestamp() instead of query.setDate(). I have checked org.hibernate.Query sources and is says:

/**
 * Bind the date (time is truncated) of a given Date object 
 * to a named query parameter.
 *
 * @param name The name of the parameter
 * @param date The date object
 */
public Query setDate(String name, Date date);

but

/**
 * Bind the date and the time of a given Date object to a named query parameter.
 *
 * @param name The name of the parameter
 * @param date The date object
 */
public Query setTimestamp(String name, Date date);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the last value of a column using HQL(Hibernate Query Language)

From Dev

How to search current date in Timestamp field of MYSQL using Hibernate Named Query (HQL)?

From Dev

hibernate HQL Query select

From Dev

Using JPQL(not HQL) with Hibernate

From Dev

HQL Query fails in Hibernate MySQL

From Dev

Hibernate HQL : no entity found for query

From Dev

Simple HQL Query and Criteria in Hibernate

From Dev

HQL Query fails in Hibernate MySQL

From Dev

Hibernate HQL : no entity found for query

From Dev

Query Hibernate or HQL not return result

From Dev

How to query filter by date using hibernate, HSQL

From Dev

Updation mysql date column using Hibernate?

From Dev

Using OPENQUERY with an Oracle database to query a date range

From Dev

Using OPENQUERY with an Oracle database to query a date range

From Dev

Hibernate HQL - include columns from PK into query

From Dev

How to query join one to many in hibernate HQL

From Dev

Oracle Linked Server in SQL Server 2012 - query with DATE column not working

From Dev

Hibernate: using shared primary key in HQL

From Dev

Casting the string column as int in hql query or criteria

From Dev

Not getting column names in the result of hql query

From Dev

Not getting column names in the result of hql query

From Dev

Hibernate query grouping by column

From Dev

HQL equivalent query to criteria query in hibernate many to many relationship?

From Dev

HQL equivalent query to criteria query in hibernate many to many relationship?

From Dev

Hibernate hql substring function is different behavior in postgres and oracle?

From Dev

Hibernate hql substring function is different behavior in postgres and oracle?

From Dev

Oracle query on time (and not date)

From Dev

Oracle query BETWEEN (date) AND (date)

From Dev

Converting Mysql TIME data type to JS Date - Hibernate HQL?

Related Related

  1. 1

    How to get the last value of a column using HQL(Hibernate Query Language)

  2. 2

    How to search current date in Timestamp field of MYSQL using Hibernate Named Query (HQL)?

  3. 3

    hibernate HQL Query select

  4. 4

    Using JPQL(not HQL) with Hibernate

  5. 5

    HQL Query fails in Hibernate MySQL

  6. 6

    Hibernate HQL : no entity found for query

  7. 7

    Simple HQL Query and Criteria in Hibernate

  8. 8

    HQL Query fails in Hibernate MySQL

  9. 9

    Hibernate HQL : no entity found for query

  10. 10

    Query Hibernate or HQL not return result

  11. 11

    How to query filter by date using hibernate, HSQL

  12. 12

    Updation mysql date column using Hibernate?

  13. 13

    Using OPENQUERY with an Oracle database to query a date range

  14. 14

    Using OPENQUERY with an Oracle database to query a date range

  15. 15

    Hibernate HQL - include columns from PK into query

  16. 16

    How to query join one to many in hibernate HQL

  17. 17

    Oracle Linked Server in SQL Server 2012 - query with DATE column not working

  18. 18

    Hibernate: using shared primary key in HQL

  19. 19

    Casting the string column as int in hql query or criteria

  20. 20

    Not getting column names in the result of hql query

  21. 21

    Not getting column names in the result of hql query

  22. 22

    Hibernate query grouping by column

  23. 23

    HQL equivalent query to criteria query in hibernate many to many relationship?

  24. 24

    HQL equivalent query to criteria query in hibernate many to many relationship?

  25. 25

    Hibernate hql substring function is different behavior in postgres and oracle?

  26. 26

    Hibernate hql substring function is different behavior in postgres and oracle?

  27. 27

    Oracle query on time (and not date)

  28. 28

    Oracle query BETWEEN (date) AND (date)

  29. 29

    Converting Mysql TIME data type to JS Date - Hibernate HQL?

HotTag

Archive