Using Joda DateTime in an Hibernate entity

998 views
Skip to first unread message

Richard

unread,
May 2, 2013, 8:30:31 AM5/2/13
to dropwiz...@googlegroups.com
Hi,

Two questions about DW (6.2) support of Joda DateTime persistence through Hibernate.

@Entity
@NamedQueries({
        @NamedQuery(
                name="com.example.project.Foo.findByDate",
                query="SELECT f FROM Foo f WHERE f.dateTime = :date"
        )
})
public class Foo {

    @Column(name="date_time", updatable = false)
    @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    private DateTime dateTime;
}

1. When creating or querying this entity, DW's logs display:

WARN  [2013-05-02 11:50:14,202] org.jadira.usertype.spi.reflectionutils.JavaTimeZoneWorkaroundHelper: Under JDK 6 it may not be possible to handle DST transitions correctly
ERROR [2013-05-02 11:50:14,202] org.jadira.usertype.spi.reflectionutils.JavaTimeZoneWorkaroundHelper: Running under a Zone that uses daylight saving time. To avoid incorrect datetimes being stored during DST transition, either update to JDK 7 or use a Timezone for the JDK without Daylight Saving Time

Everything works fine, or at least seems to, but this warning/error message makes me wondering if using Joda Time with JPA entities is realy a good idea (and if it's not, why DW includes Jadira and such ?).

2. How to query for all Foos at a given date (i.e. droping time part)? The named query "com.example.project.Foo.findByDate" doesn't retrieve entities, using greater comparison works.

Neither


return list(
                namedQuery("com.example.project.Foo.findByDate")
                        .setDate("date", dateTime.toDate())
        );

nor

return list(
                namedQuery("com.example.project.Foo.findByDate")
                        .setParameter("date", dateTime.toDate(), DateType.INSTANCE)
        );

works (no result returned but entries exists for this date).

Any help apreciated, thanks in advance!

Richard

unread,
May 2, 2013, 9:57:07 AM5/2/13
to dropwiz...@googlegroups.com
#2 solved using

return list(
                namedQuery("com.example.project.Foo.findByDate")
                        .setParameter("beginTime", dateTime.withTime(0, 0, 0, 0))
                        .setParameter("endTime", dateTime.withTime(23, 59, 59, 999))
        );

and

@NamedQuery(
                name="com.example.project.Foo.findByDate",
                query="SELECT f FROM Foo f WHERE f.dateTime >= :beginTime AND f.dateTime <= :endTime"
        )

but there may be a more idiomatic way.

Question 1 still remains.

Matt Brown

unread,
May 3, 2013, 10:46:09 AM5/3/13
to dropwiz...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages