openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From garpinc <garp...@yahoo.com>
Subject Re: Failing to persist a field where the type is XMLGregorianCalendar.
Date Fri, 20 Jul 2012 17:09:13 GMT
here are the 2 classes involved in the annotation
package com.cambridgesemantics.anzo.relationalreplicator.openjpaplugin;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;

import org.apache.openjpa.jdbc.kernel.JDBCStore;
import org.apache.openjpa.jdbc.meta.ValueMapping;
import org.apache.openjpa.jdbc.meta.strats.AbstractValueHandler;

public abstract class XGCAbstractValueHandler extends AbstractValueHandler {
    @Override
    public Object toObjectValue(ValueMapping vm, Object val) {
        if (val == null)
            return null;
        try {
            return
DatatypeFactory.newInstance().newXMLGregorianCalendar(val.toString());
        } catch (DatatypeConfigurationException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Object toDataStoreValue(ValueMapping vm, Object val, JDBCStore
store) {
        if (val == null || !(val instanceof
javax.xml.datatype.XMLGregorianCalendar))
            return null;

        javax.xml.datatype.XMLGregorianCalendar date =
(javax.xml.datatype.XMLGregorianCalendar) val;
        return date.toGregorianCalendar().getTime();
    }

}


package com.cambridgesemantics.anzo.relationalreplicator.openjpaplugin;

import org.apache.openjpa.jdbc.identifier.DBIdentifier;
import org.apache.openjpa.jdbc.meta.ValueMapping;
import org.apache.openjpa.jdbc.schema.Column;
import org.apache.openjpa.jdbc.schema.ColumnIO;
import org.apache.openjpa.jdbc.sql.DBDictionary;
import org.apache.openjpa.meta.JavaTypes;

public class XGCTimestampValueHandler extends XGCAbstractValueHandler {

    private static final XGCTimestampValueHandler _instance = new
XGCTimestampValueHandler();

    /**
     * Singleton instance.
     */
    public static XGCTimestampValueHandler getInstance() {
        return _instance;
    }

    @Deprecated
    @Override
    public Column[] map(ValueMapping vm, String name, ColumnIO io, boolean
adapt) {
        DBDictionary dict = vm.getMappingRepository().getDBDictionary();
        DBIdentifier colName = DBIdentifier.newColumn(name, dict != null ?
dict.delimitAll() : false);
        return map(vm, colName, io, adapt);
    }

    public Column[] map(ValueMapping vm, DBIdentifier name, ColumnIO io,
boolean adapt) {
        Column col = new Column();
        col.setIdentifier(name);
        col.setJavaType(JavaTypes.DATE);
        col.setType(java.sql.Types.TIMESTAMP);
        return new Column[] { col };
    }

}




--
View this message in context: http://openjpa.208410.n2.nabble.com/Failing-to-persist-a-field-where-the-type-is-XMLGregorianCalendar-tp7580665p7580674.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Mime
View raw message