I have an Oracle table with an SDO_GEOMETRY column and I am trying to use EclipseLink JPA2 2.6.1 for persistence. My entity class uses a JTS Geometry for geometry objects and I have written a AttributeConverter to convert from a SDO_GEOMETRY to a JTS Geometry. This works well and I can read and write the geometries from the database. The problem I am having is that I cannot persist a null JTS Geometry. I get the following error:
ORA-00932: inconsistent datatypes: expected MDSYS.SDO_GEOMETRY got CHAR
Not sure if I am doing something wrong or if there is a bug in EclipseLink or Oracle.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="mainPersistence">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>persistence.v1.dao.jpa2.converters.GeometryConverter</class>
<class>persistence.v1.dto.AuthorizationDto</class>
<properties>
<property name="eclipselink.weaving" value="false"/>
</persistence-unit>
Entity class
package persistence.v1.dto;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import persistence.v1.dao.jpa2.converters.GeometryConverter;
import com.vividsolutions.jts.geom.Geometry;
@Entity
@Table(name="AUTHORIZATION")
public class AuthorizationDto {
private String authorizationGuid;
private Geometry authorizationGeometry;
public AuthorizationDto() {
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "system-uuid")
@Column(name="AUTHORIZATION_GUID", nullable=false)
public String getAuthorizationGuid() {
return this.authorizationGuid;
}
public void setAuthorizationGuid(String authorizationGuid) {
this.authorizationGuid = authorizationGuid;
}
@javax.persistence.Convert(converter=GeometryConverter.class)
@Column(name="AUTHORIZATION_GEOMETRY")
public Geometry getAuthorizationGeometry() {
return this.authorizationGeometry;
}
public void setAuthorizationGeometry(Geometry authorizationGeometry) {
this.authorizationGeometry = authorizationGeometry;
}
}
GeometryConverter class
package persistence.v1.dao.jpa2.converters;
import java.sql.SQLException;
import java.sql.Struct;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import oracle.jdbc.OracleConnection;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.oracle.OraReader;
import com.vividsolutions.jts.io.oracle.OraWriter;
@Converter(autoApply = true)
public class GeometryConverter implements AttributeConverter<Geometry, Object> {
private static ThreadLocal<OracleConnection> currentConnection = new ThreadLocal<>();
public static void setConnection(OracleConnection connection) {
currentConnection.set(connection);
}
private Geometry toGeometry(Object geometryData) {
Geometry result = null;
OraReader reader = new OraReader();
try {
StructDescriptor descriptor = new StructDescriptor(
"MDSYS.SDO_GEOMETRY", currentConnection.get());
STRUCT geometryStruct = new STRUCT(descriptor,
currentConnection.get(), (Object[]) geometryData);
result = reader.read(geometryStruct);
} catch (SQLException e) {
logger.warn("Cound not create geometry from database column", e);
throw new RuntimeException(e);
}
return result;
}
private Struct fromGeometry(Geometry geometry) {
try {
return new OraWriter().write(geometry, currentConnection.get());
} catch (SQLException e) {
logger.warn("Cound not create database column from geometry "
+ geometry.toText(), e);
throw new RuntimeException(e);
}
}
@Override
public Object convertToDatabaseColumn(Geometry geometry) {
logger.debug("<convertToDatabaseColumn");
Object result = null;
if(geometry!=null) {
result = fromGeometry(geometry);
}
logger.debug(">convertToDatabaseColumn "+result);
return result;
}
@Override
public Geometry convertToEntityAttribute(Object geometryData) {
logger.debug("<convertToEntityAttribute");
Geometry result = null;
if(geometryData!=null) {
result = toGeometry(geometryData);
}
logger.debug(">convertToEntityAttribute "+result);
return result;
}
}
Thanks