How to map Joda Times with Hibernate

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.

One thought on “How to map Joda Times with Hibernate

  1. Pingback: Joda with JPA « Objectopia

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 )

Google photo

You are commenting using your Google 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