【发布时间】:2015-12-11 18:13:23
【问题描述】:
我有一个带有 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;
}
}
谢谢
【问题讨论】:
标签: java oracle jpa eclipselink persistence.xml