Joda DateTime in Hibernate Queries and JPA Entities

If you use JodaTime in a JPA Entity, you’ll need to observe the following things:

1) Your Entity member should declare the following annotations:

@Column(name = “cutoff”)

@Type(type=”org.joda.time.contrib.hibernate.PersistentDateTime”)

@Getter

@Setter

private DateTime cutOff;

2) If you want to query that field, you should convert DateTime to Date in your params:

StringBuilder query = new StringBuilder(“from IPLWeightAggregate “);

query.append(” where cutoff between :from and :to”);

Query statement = sessionFactory.createQuery(queryString, IPLWeightAggregate.class);

statement.setParameter(“from”, date.minusDays(1).toDate());

statement.setParameter(“to”, date.plusDays(1).toDate());

Otherwise, your query won’t return any results – not even an error message.

Advertisements
This entry was posted in Enterprise Java, Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s