NerdGerl

How to map Joda Times with Hibernate

Posted by: nerdgerl on: June 18, 2009

I recently came across this problem when I added Hibernate to a project. If you have an entity like:

@Entity
@Table(name = "upload")
public class Upload {

 @Id
 @Column(name = "upload_id")
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Integer uploadId;

 @Column(name = "user_id")
 private String userId;

 @Transient
 private Integer rowsExported;

 @Column(name = "adjustment_type")
 @Enumerated(EnumType.STRING)
 private ManualAdjustmentType adjustmentType;

 @Column(name = "upload_time_stamp")
 private DateTime uploadTimeStamp;

 @Column(name = "status")
 @Enumerated(EnumType.STRING)
 private UploadStatus status;

}

You’ll likely get an sql exception when you try to persist. Wha you need to do is add the joda annotations jar to your classpath and then update your entity as follows:
@Column(name = "cut_off_date")
<strong>@Type(type = "org.joda.time.contrib.hibernate.PersistentLocalDate")</strong>
 private LocalDate cutOffDate;

 @Column(name = "upload_time_stamp")
<strong>@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")</strong>
 private DateTime uploadTimeStamp;</pre>

More joda mappings.

If you are unlucky enough to be dealing with dates stored as Strings in the database, take a look at this post.

Tags: ,

1 Response to "How to map Joda Times with Hibernate"

[...] Posted by Chris Clark on July 10, 2009 I have to work with a few legacy databases that are used by several different apps (apparently the database is a perfect integration point), and I want to use the Joda api in my persistence. In this example the date is actually stored as a string, genius! I’m going to use LocalDate as the string doesn’t store the time element. If you are lucky enough and the database is actually storing date time check this post out Joda With DateTime [...]

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.