Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[eclipselink-users] problem with converter

Hi!

i setted a converter to converter a property of type "java.util.Date" to String, since the database procedure take String as parameter.


        DirectToFieldMapping directToFieldMapping = (DirectToFieldMapping) descriptor
                .getMappingForAttributeName(TreatmentEvent.PROP_DATE);
        directToFieldMapping.setConverter(new DateConverter(DateUtils.DATE_TIME_PATTERN_DB_INPUT));

hier ist my converter class
public class DateConverter implements Converter {

    /**
     *
     */
    private static final long serialVersionUID = 4686174423776046431L;

    private String patternToUse;

    public DateConverter() {
        this(DateUtils.DATE_PATTERN_DB_INPUT);
    }

    public DateConverter(String formatToUse) {
        this.patternToUse = formatToUse;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.persistence.mappings.converters.Converter#convertDataValueToObjectValue(java.lang.Object,
     *      org.eclipse.persistence.sessions.Session)
     */
    public Object convertDataValueToObjectValue(Object dataValue,
            Session session) {
        return dataValue;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.persistence.mappings.converters.Converter#convertObjectValueToDataValue(java.lang.Object,
     *      org.eclipse.persistence.sessions.Session)
     */
    public Object convertObjectValueToDataValue(Object objectValue,
            Session session) {
        Date date = (Date) objectValue;
        return DateUtils.changeDateByPattern(date, patternToUse);
    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.persistence.mappings.converters.Converter#initialize(org.eclipse.persistence.mappings.DatabaseMapping,
     *      org.eclipse.persistence.sessions.Session)
     */
    public void initialize(DatabaseMapping mapping, Session session) {
        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     *
     * @see org.eclipse.persistence.mappings.converters.Converter#isMutable()
     */
    public boolean isMutable() {
        return true;
    }

    public String toString() {
        return super.toString() + "(" + patternToUse + ")";
    }

}

But after eclipselink transform the java.date.util using the method #convertToDataValue, somewhere in the processing the value is setted back to Date and the Date String eclipselink tries to persist is : "0028-05-30 00:00:00.0"  the eclipselink logger printed it out...

am i using converter on a wrong way?

thanks for any help

Rodrigue

Back to the top