customerized convert from field type to DB field's type

@LastModifiedDate

@Convert(converter = LocalDateTime2TimestampConverter.class)

@Slf4j

public class LocalDateTime2TimestampConverter implements

AttributeConverter<LocalDateTime, Timestamp> {

 

@Override

public Timestamp convertToDatabaseColumn(LocalDateTime ld) {

return ld == null ? null : new Timestamp(ld.toDateTime().getMillis());

}

 

@Override

public LocalDateTime convertToEntityAttribute(Timestamp ts) {

if (ts != null) {

            try {

                return LocalDateTime.fromDateFields(ts);

            } catch (IllegalArgumentException ex) {

                log.warn("Can't convert {} to LocalDate", ts, ex);

            }

        }

        return null;

}

 

}

 

posted @ 2015-04-24 13:01  samu  阅读(192)  评论(0编辑  收藏  举报