我需要向 MySQL 数据库 (v5.5.28) 的条目添加地理空间信息,具体来说是“点”。我尝试使用简单的代码将创建的列“location”映射到我的 java EJB 实体 ServiceInfo 的相应属性:
@Column(name = "location")
private com.vividsolutions.jts.geom.Point location;
但是,这会导致以下错误:
Exception [EclipseLink-66] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Could not deserialize object from byte array.
Internal Exception: java.io.StreamCorruptedException: invalid stream header: 00000000
Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[location-->ServiceInfo.location]
Descriptor: RelationalDescriptor(ies.persistence.entity.ServiceInfo --> [DatabaseTable(ServiceInfo)])
at org.eclipse.persistence.exceptions.DescriptorException.notDeserializable(DescriptorException.java:1218)
at org.eclipse.persistence.mappings.converters.SerializedObjectConverter.convertDataValueToObjectValue(SerializedObjectConverter.java:72)
...
我认为问题是 java mysql 连接器 (v5.1.22) 不支持地理空间信息,这让我感到惊讶,因为数据库支持。有人可以确认是这种情况,还是告诉我我可能做错了什么并指出正确的方向?
我在 Netbeans 7.2 中工作,使用 JDK 1.7 和 GlassFish Server 3.1.2
最佳答案
好的,所以我最终通过对我的持久层进行以下更改来解决我的问题:
我使用空间扩展 PostGIS 从 MySQL 数据库切换到 PostgeSQL 数据库。这是因为 MySQL 仅部分实现了 OpenGIS 规范。 接下来,我必须将 EclipseLink (JPA 2.0)(默认)的持久性单元更改为 Hibernate (JPA 2.0)。
要包含在我的项目和 glassfish 的类路径中的 Jar(否则似乎无法找到它们。我不得不将它们复制到 domain1 的根目录)是:
- postgresql-9.2-1002.jdbc4.jar
- postgis-jdbc-2.1.0SVN.jar
- Hibernate 4.1.8(所有必需的 jar,可选的 ehcache 和 JPA 的 hibernate-entitymanager-4.1.8.Final.jar...这些都包含在发布包中)
- hibernate-spatial-4.0-M1.jar(hibernate-spatial-1.1.1.jar 和 hibernate-spatial-postgis-1.1.1.jar 与 Hibernate 4.x 不兼容!)。
- jts-1.8.jar
生成的 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="IES-ejbPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/postgresql_iesdb3</jta-data-source>
<class>ies.persistence.entity.ServiceInfo</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.spatial.dialect.postgis.PostgisDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
用于我的 EJB 实体的注释是:
@Type(type="org.hibernate.spatial.GeometryType")
@Column(name = "location", columnDefinition="Geometry")
private com.vividsolutions.jts.geom.Geometry location;
我们现在可以使用 Hibernate + JPA + PostgeSQL/PostGIS 进行适当的映射。
关于java - 将 MySQL 几何图形映射到 EJB 实体时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13459027/