好的,最后我只使用EclipseLink @Converter
解决了这个问题。我就是这样做的。我也欢迎任何关于这种方式的评论:-
ItemMaster 实体:-
public class ItemMaster {
@Converter(
name="periodConverter",
converterClass = com.joda.converter.PeriodConverter.class
)
@Convert("periodConverter")
@Column(name = "expiry_limit", length = 2000)
private Period expiryLimit;
/** Constructors **/
public Period getExpiryLimit() {
return expiryLimit;
}
public void setExpiryLimit(Period expiryLimit) {
this.expiryLimit = expiryLimit;
}
}
PeriodConverter.class: -
public class PeriodConverter implements Converter {
private Logger log;
private static final long serialVersionUID = 1L;
@Override
public Object convertDataValueToObjectValue(Object str, Session session) {
if (str == null) {
log.info("convertDataValueToObjectValue returning null");
return null;
}
return new Period(str);
}
@Override
public Object convertObjectValueToDataValue(Object period, Session session) {
/** str format should be: - `PyYmMwWdDThHmMsS` **/
if (period == null) {
log.info("convertObjectValueToDataValue returning null");
return null;
}
return period.toString();
}
@Override
public void initialize(DatabaseMapping mapping, Session session) {
log = Logger.getLogger("com.joda.converter.PeriodConverter");
((AbstractDirectMapping) mapping).setFieldType(java.sql.Types.VARCHAR);
}
@Override
public boolean isMutable() {
return true;
}
}