Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [eclipselink-users] Converters, non-standard datatypes, and nulls

Just one note:
Because of me using postgis instead of vividsolutions the converter
class looks like that:

package de.fhg.fokus.openride.helperclasses.converter;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.sessions.Session;
import org.postgresql.geometric.PGpoint;

import java.util.logging.Logger;
import org.postgis.Point;

/**
 * Eclipselink converter to/from postgis Point / PGPoint
 *
 *
 */
public class PointConverter implements Converter {
  private static final long serialVersionUID = -5938037316595234421L;
  static Logger log = Logger.getLogger(PointConverter.class.getName());

  public Point convertDataValueToObjectValue(Object dataValue, Session
session) {
    if (dataValue == null) {
      return null;
    }
    else if (dataValue instanceof PGpoint) {
        System.out.println("PGPopint: "+dataValue);
      return new Point(((PGpoint)dataValue).x, ((PGpoint)dataValue).y);
    }
    else {
      log.severe("dataValue not instance of PGpoint");
      return null;
    }
  }

  public PGpoint convertObjectValueToDataValue(Object objectValue,
Session session) {
    if (objectValue == null) {
      // can't return null here it will attempt to store as varchar -
this results in a POINT(0,0), which is incorrect
        // init changed -> should be working!
      return null;
    }
    else if (objectValue instanceof Point) {
      return new PGpoint(((Point)objectValue).getX(),
((Point)objectValue).getY());
    }
    else {
      log.severe("objectValue not instance of Point");
      return new PGpoint();
    }
  }

  public void initialize(DatabaseMapping dm, Session session) {
    dm.getField().setSqlType(java.sql.Types.OTHER);
  }


  public boolean isMutable() {
    return false;
  }
}


PS: this is deployed on a glassfishV3 using PostGres8.3+PostGis 1.3.6

-----Original Message-----
From: eclipselink-users-bounces@xxxxxxxxxxx
[mailto:eclipselink-users-bounces@xxxxxxxxxxx] On Behalf Of Mike Traum
Sent: Donnerstag, 7. Januar 2010 22:00
To: EclipseLink User Discussions
Subject: [eclipselink-users] Converters, non-standard datatypes, and
nulls

Hi,
I've been working with a user on an active thread about writing a 
converter for a spatial datatype under postgres/postgis. This brought up

an issue I've had in the past. It seems that when you're dealing with 
non-standard types (not a normal sql type), everything is generally 
fine. But, there always seems to be a problem when trying to store a 
null value for that type - postgres throws an exception about the column

being of type varchar instead of the actual type.

I did a little debugging, and it seems the DatabaseField.sqlType  is not

being set.  If  I add the code below to the converter (sets the sqlType 
to java.sql.Types.OTHER), it seems to always work. Should this be a fix 
in eclipselink (or, even better, set the type to java.sql.Types.NULL 
when the value is null)?

  public void initialize(DatabaseMapping dm, Session session) {
    dm.getField().setSqlType(java.sql.Types.OTHER);
  }

Thanks,
Mike

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


Back to the top