Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Custom Conversion(How can I implement a custom conversion in a database platform)
Custom Conversion [message #1196535] Tue, 19 November 2013 13:13 Go to next message
Peter Kullmann is currently offline Peter KullmannFriend
Messages: 240
Registered: July 2009
Senior Member
We are using EclipseLink JPA with an OpenEdge database and we have a problem with a timestamp field which is also used as version:

    @Basic()
    @Column(name = "mutdatime")
    @Version
    private Timestamp changedOn = null;


The mutdatime field in the database is defined as DATETIME-TZ which is a timestamp field that includes timezone information. The JDBC driver (by DataDirect) maps the field as a String field and produces timestamps like this:
2013-10-09 11:05:58:528 + 02:00


The default platform of EL works pretty well with these timestamps but we noticed problems as soon as our daylight saving changed in october.

We would like to do the java.sql.Timestamp <--> String conversion ourselves in order to fix issues with the time zone. For this we created a subclass of org.eclipse.persistence.platform.database.DatabasePlatform and have overridden the following methods:
    @Override
    public Object convertObject(Object sourceObject, Class javaClass)
            throws ConversionException {
        if (sourceObject instanceof String && javaClass.equals(Timestamp.class)) {
            // do our own conversion
        }

        return super.convertObject(sourceObject, javaClass);
    }


    @Override
    public Object convertToDatabaseType(Object value) {
        if (value instanceof Timestamp) {
            // do our own conversion
        }
        return super.convertToDatabaseType(value);
    }

    @Override
    public void setParameterValueInDatabaseCall(Object parameter,
            PreparedStatement statement, int index, AbstractSession session)
            throws SQLException {
        if (parameter instanceof Timestamp) {
            String converted = (String) convertToDatabaseType(parameter);
            statement.setString(index, converted);
            return;
        }

        super.setParameterValueInDatabaseCall(parameter, statement, index,
                session);
    }


This works but I'm not sure whether I have gotten the right methods. For instance, overriding setParameterValueInDatabaseCall seems unnecessary because there already is a conversion for this direction in convertToDatabaseType, but it is not used except for the log. Can anyone point me to an example or to documentation for this kind of platform adaption?


Thanks & best regards,
Peter
Re: Custom Conversion [message #1221275 is a reply to message #1196535] Tue, 17 December 2013 09:30 Go to previous messageGo to next message
Peter Kullmann is currently offline Peter KullmannFriend
Messages: 240
Registered: July 2009
Senior Member
Any hints on how to code a DatabasePlatform?

Regards,
Peter
Re: Custom Conversion [message #1221374 is a reply to message #1221275] Tue, 17 December 2013 14:52 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
There isn't much in the way of documentation, but for examples, you can use the Oracle9Platform class which does similar stuff for the Oracle TIMESTAMPTZ and other Oracle custom types. What you have looks correct to me, but I can't say definitively without testing. The setParameterValueInDatabaseCall might be needed as binding will take the object as is and pass it to the driver without converting it, as it does not know what type to convert it to.

Another solution is to use a converter on the mapping that can handle converting the Timestamp to/from a String. A simple example is shown here: http://eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_converter.htm and here http://wiki.eclipse.org/EclipseLink/Release/2.5/JPA21#A_Converter_annotation_example


Best Regards,
Chris
Re: Custom Conversion [message #1229007 is a reply to message #1221374] Wed, 08 January 2014 14:32 Go to previous messageGo to next message
Peter Kullmann is currently offline Peter KullmannFriend
Messages: 240
Registered: July 2009
Senior Member
Thanks, Chris. I tried with the converter but didn't manage to make it work. I'm using the custom platform now and it works actually quite well.
Re: Custom Conversion [message #1287210 is a reply to message #1196535] Mon, 07 April 2014 14:32 Go to previous message
Juan Alvarez Estrada is currently offline Juan Alvarez EstradaFriend
Messages: 1
Registered: April 2014
Junior Member
Good morning,

Is there any documentation that explains how to setup an OpenEdge Database
for working with JPA?

I've search in the OpenEdge Documentacion, OpenEdge KBase and couldn't find
any step by step instructions.

Where did you find how to make this connection?

Have a nice day and thanks for your cooperation.

JAE
Previous Topic:CacheType.NONE
Next Topic:Insert Duplicate key Exception - Eclipselink
Goto Forum:
  


Current Time: Sat Apr 20 00:23:07 GMT 2024

Powered by FUDForum. Page generated in 0.03380 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top