Skip to content
Advertisement

Insert Null Struct using EclipseLink JPA

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

JavaScript

Entity class

JavaScript

GeometryConverter class

JavaScript

Thanks

Advertisement

Answer

The exception is being caused by EclipseLink using VARCHAR as the default for unknown types. Oddly the AttibuteConverter.convertToEntityAttribute method receives an Object array instead of a Struct for the SDO_GEOMETRY type but expects a Struct to be returned by the AttibuteConverter.convertToDatabaseColumn method. This is probably a symptom of the underlying problem. There may be a way to tell EclipseLink what the type is using annotations or some other configuration but I could not discover how so this is my workaround.

I created an EclipseLink SessionEventListener that uses the prelogin method to identify Entity methods that return the Geometry type. I use the DatabaseField object to create a new ObjectRelationalDatabaseField then I set the sqlType property to Struct and the sqlTypeName property to “MDSYS.SDO_GEOMETRY”. then I update the mapping with the new ObjectRelationalDatabaseField object. The EclipseLink code now has enough information to correctly set the Statement.setNull (String parameterName, int sqlType, String typeName) method.

The SessionEventListener is configured in the persistence.xml and the null insert is now successful.

GeometryInitializerSessionEventListener class

JavaScript

Persistence.xml

JavaScript

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement