views:

101

answers:

1

It looks like I have to implement com.ibatis.sqlmap.client.extensions.TypeHandlerCallback for both DateTime and LocalDateTime Joda types. This isn't a big deal, but if it's implemented somewhere else I'd rather just use that.

A: 

There are no clean and flexible solutions, IMO. Jodatime dates are more flexible than the JDBC native types (java.sql.Date,java.sql.Timestamp...) and if you map to them you might be losing information (timezones). An "full" implementation, (perhaps bypassing the getDate()/getTimestamp() etc JDBC methods, using strings for example) would depend on your JDBC driver and database (and practically no database has datetime type as expressive as Jodatime's).

Here's a rather trivial implementation I did once for LocalDateTime. Not very tested, and it assumes your DB timestamps represent local datetimes.

public Object getResult(ResultGetter getter) throws SQLException {
   Timestamp date = getter.getTimestamp();
   if(date == null) return null; 
   LocalDateTime ldt = null;
   try {
      ldt = LocalDateTime.fromDateFields(date);
   } catch(IllegalArgumentException e) {
      throw new SQLException("illegal value for a LocalDateTime : " + date);
   }
   return ldt; 
}

public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
   java.sql.Timestamp date = null; 
   if(parameter != null) { 
      LocalDateTime ldt = (LocalDateTime) parameter;
      date = new Timestamp(ldt.toDateTime().getMillis());
   } 
   setter.setTimestamp(date);
}
leonbloy