Eelke Eelke - 1 year ago 72
Java Question

JodaTime with JPA, PostgreSQL and NULL values

I'm trying to persists JodaTime DateTime fields with JPA to PostgreSQL but run into troubles with null pointers to database NULL values.

I'm working with the NetBeans 7 beta 2 IDE. The persistence implementation is EclipseLink 2.2.0 and I'm using an EclipseLink Converter to get the mapping to work. Here is the declaration of my field:

@Column(columnDefinition="TIMESTAMP WITH TIME ZONE")
private DateTime testdate;

The converter class:

public class DateTimeConverter implements Converter {

private Logger log;
private static final long serialVersionUID = 1L;

public Object convertObjectValueToDataValue(Object o, Session sn) {
if (o == null) {"convertObjectValueToDataValue returning null");
return null;
return ((DateTime)o).toDate();

public Object convertDataValueToObjectValue(Object o, Session sn) {
if (o == null) {"convertDataValueToObjectValue returning null");
return null;
return new DateTime(o);

public boolean isMutable() {
return true;

public void initialize(DatabaseMapping dm, Session sn) {
log = Logger.getLogger("ejb.util.DateTimeConverter");


This works fine as long as there is an actual DateTime set. But as soon as it is not set EclipseLink seems to assume a string type and postgresql starts complaining about the value being of the type character varying. I assume this is because the converter class is returning a null pointer instead of a date object and EclipseLink falls back on a default.

Is there a way to get this to work short of switching to plain java.util.Date?

Answer Source

When a @Converter is used, EclipseLink does not know the type, so you need to initialize it.

In your initialize(DatabaseMapping dm, Session sn) method you need to set the type,

// or, dm.setFieldClassification(java.sql.Timestamp.class);