我有一个带有SDO_GEOMETRY列的Oracle表,我正在尝试使用EclipseLink JPA2 2.6.1来实现持久性。我的实体类对几何对象使用JTS几何,我编写了一个AttributeConverter来将SDO_GEOMETRY转换为JTS几何。这很好用,我可以从数据库中读取和写入几何。我遇到的问题是我不能坚持一个空的JTS几何。我收到以下错误:
ORA-00932:不一致的数据类型:预期的MDSYS.SDO_GEOMETRY得到了CHAR
不确定我是在做错事还是EclipseLink或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>
实体类
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类
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;
}
}
由于
答案 0 :(得分:0)
异常是由EclipseLink使用VARCHAR作为未知类型的默认值引起的。奇怪的是,AttibuteConverter.convertToEntityAttribute方法接收一个Object数组而不是SDO_GEOMETRY类型的Struct,但是期望AttibuteConverter.convertToDatabaseColumn方法返回一个Struct。这可能是潜在问题的症状。可能有一种方法告诉EclipseLink使用注释或其他配置的类型是什么,但我无法发现这是我的解决方法。
我创建了一个EclipseLink SessionEventListener,它使用prelogin方法来识别返回Geometry类型的Entity方法。我使用DatabaseField对象创建一个新的ObjectRelationalDatabaseField然后我将sqlType属性设置为Struct,将sqlTypeName属性设置为“MDSYS.SDO_GEOMETRY”。然后我用新的ObjectRelationalDatabaseField对象更新映射。 EclipseLink代码现在有足够的信息来正确设置Statement.setNull(String parameterName,int sqlType,String typeName)方法。
SessionEventListener在persistence.xml中配置,空插入现在成功。
GeometryInitializerSessionEventListener类
package persistence.v1.dao.jpa2.listeners;
import java.lang.reflect.Method;
import java.sql.Types;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.DirectToFieldMapping;
import org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField;
import org.eclipse.persistence.sessions.Session;
import org.eclipse.persistence.sessions.SessionEvent;
import org.eclipse.persistence.sessions.SessionEventListener;
import com.vividsolutions.jts.geom.Geometry;
public class GeometryInitializerSessionEventListener implements SessionEventListener {
// Omitting empty interface methods
@Override
public void preLogin(SessionEvent event) {
Session s = event.getSession();
@SuppressWarnings("rawtypes")
Map<Class, ClassDescriptor> descriptorMap = s.getDescriptors();
for (@SuppressWarnings("rawtypes") Entry<Class, ClassDescriptor> entry : descriptorMap.entrySet()) {
Class<?> cls = entry.getKey();
ClassDescriptor desc = entry.getValue();
Vector<DatabaseMapping> mappings = desc.getMappings();
for(DatabaseMapping mapping:mappings) {
if (mapping.getAttributeAccessor() instanceof MethodAttributeAccessor) {
MethodAttributeAccessor maa = (MethodAttributeAccessor) mapping.getAttributeAccessor();
String methodName = maa.getGetMethodName();
Method method;
try {
method = cls.getMethod(methodName, new Class[]{});
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (SecurityException e) {
throw new RuntimeException(e);
}
Class<?> returnType = method.getReturnType();
if(Geometry.class.equals(returnType)) {
DirectToFieldMapping directToFieldMapping = (DirectToFieldMapping) mapping;
ObjectRelationalDatabaseField objectRelationalDatabaseField = new ObjectRelationalDatabaseField(mapping.getField());
objectRelationalDatabaseField.setSqlType(Types.STRUCT);
objectRelationalDatabaseField.setSqlTypeName("MDSYS.SDO_GEOMETRY");
directToFieldMapping.setField(objectRelationalDatabaseField);
}
}
}
}
}
}
的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"/>
<property name="eclipselink.session-event-listener" value="persistence.v1.dao.jpa2.listeners.GeometryInitializerSessionEventListener"/>
</properties>
</persistence-unit>
答案 1 :(得分:0)
使用方法注释时,上述答案是正确的。使用字段批注时,请使用以下GeometryInitializerSessionEventListener实现:
package persistence.dao.jpa2.listeners;
import java.lang.reflect.Field;
import java.sql.Types;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.DirectToFieldMapping;
import org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField;
import org.eclipse.persistence.sessions.Session;
import org.eclipse.persistence.sessions.SessionEvent;
import org.eclipse.persistence.sessions.SessionEventListener;
import com.vividsolutions.jts.geom.Geometry;
public class GeometryInitializerSessionEventListener implements SessionEventListener
{
@Override
public void preLogin(SessionEvent event)
{
Session s = event.getSession();
@SuppressWarnings("rawtypes")
Map<Class, ClassDescriptor> descriptorMap = s.getDescriptors();
for (@SuppressWarnings("rawtypes")
Entry<Class, ClassDescriptor> entry: descriptorMap.entrySet())
{
ClassDescriptor desc = entry.getValue();
Vector<DatabaseMapping> mappings = desc.getMappings();
for (DatabaseMapping mapping: mappings)
{
if (mapping.getAttributeAccessor() instanceof InstanceVariableAttributeAccessor)
{
InstanceVariableAttributeAccessor ivaa = (InstanceVariableAttributeAccessor)mapping.getAttributeAccessor();
try
{
Field field = getFieldInHierarchy(desc.getJavaClass(), ivaa.getAttributeName());
if (Geometry.class.equals(field.getType()))
{
DirectToFieldMapping directToFieldMapping = (DirectToFieldMapping)mapping;
ObjectRelationalDatabaseField objectRelationalDatabaseField = new ObjectRelationalDatabaseField(mapping.getField());
objectRelationalDatabaseField.setSqlType(Types.STRUCT);
objectRelationalDatabaseField.setSqlTypeName("MDSYS.SDO_GEOMETRY");
directToFieldMapping.setField(objectRelationalDatabaseField);
}
}
catch (IllegalArgumentException e)
{
throw new IllegalStateException(e);
}
}
}
}
}
private static Field getFieldInHierarchy(Class<?> clazz, String fieldName)
{
for (Class<?> c = clazz; c != null; c = c.getSuperclass())
{
try
{
return c.getDeclaredField(fieldName);
}
catch (final NoSuchFieldException e)
{
// continue, trying parent
}
catch (Exception e)
{
throw new IllegalArgumentException("Cannot access field " + clazz.getName() + "." + fieldName, e);
}
}
throw new IllegalArgumentException("Cannot find field " + clazz.getName() + "." + fieldName);
}
}