Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use joda time with JPA (eclipselink)?

I tried to use the DataTime in my entity class. Adding @Temporal(TemporalType.DATE) above the field, I got the error saying "The persistent field or property for a Temporal type must be of type java.util.Date, java.util.Calendar or java.util.GregorianCalendar". I can introduce the conversion back and forth; using setters and getters as follows:

@Temporal(TemporalType.DATE)
private Calendar attendanceDate;

public DateTime getAttendanceDate() {
    return new DateTime(this.attendanceDate);
}

public void setAttendanceDate(DateTime attendanceDate) {
    this.attendanceDate = attendanceDate.toCalendar(Locale.getDefault());
}

but I hope eclipselink to do it for me. I have gone thro' Persist Joda-time's DateTime via Hibernate. The answer suggesting to use hibernate, but I have to use eclipselink. I can use the DateTime object in my entity class with DB representation as BLOB, but I need it as Date. Is there anything like jodatime-eclipselink? Or any other suggestion? Thanks for the help.

like image 962
Ahamed Avatar asked Mar 20 '12 08:03

Ahamed


People also ask

Does JPA support LocalDateTime?

Why does JPA not support LocalDate and LocalDateTime? The answer is simple, JPA 2.1 was released before Java 8 and the Date and Time API simply didn't exist at that point in time. Therefore the @Temporal annotation can only be applied to attributes of type java.

What is Joda-time format?

Joda-Time provides a comprehensive formatting system. There are two layers: High level - pre-packaged constant formatters. Mid level - pattern-based, like SimpleDateFormat.

Is Joda-time deprecated?

So the short answer to your question is: YES (deprecated).

What is Joda Java?

Joda-Time is the most widely used date and time processing library, before the release of Java 8. Its purpose was to offer an intuitive API for processing date and time and also address the design issues that existed in the Java Date/Time API.


2 Answers

Basic the link defines an EclipseLink Converter to convert from Joda DateTime to java.sql.Timestamp or Date. You could use it, or define your own converter and use it through @Convert, @Converter in EclipseLink.

For DDL creation, the converter should define the initialize method and set the type on the mapping's field to java.sql.Timestamp.

Please log a bug (or vote for the existing one) on this in EclipseLink, we should have support for Joda.

like image 104
James Avatar answered Sep 29 '22 09:09

James


I Try use joda-time-eclipselink-integration, but don't work, problably I made something wrong,

So I made more researchs and i found this link http://jcodehelp.blogspot.com.br/2011/12/persist-joda-datetime-with-eclipselink.html, they use @Converter annotation to convert the Joda Date Time.

I Try and works for me, I hope, works for you too.

like image 21
Hugo Prudente Avatar answered Sep 29 '22 10:09

Hugo Prudente