Skip to main content

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

Hi!

i solved the problem by adding the @convert annotation on the property to convert (here date). Is it possible to have the same effect (@convert) using java, for example a setter method?

Since i'm architectural fetishist, i'am avoiding to have eclipselink's annotation in the domain model explitely declared. I'm trying to only use JPA annotations in the domain model, so i may change the JPA implementation like i want in the future.

Thanks for any help

On Mon, Dec 22, 2008 at 2:57 PM, Rodrigue Lagoue <rlagoue@xxxxxxxxxxxxxx> wrote:
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