简体   繁体   English

将MySQL Geometry映射到EJB实体时出错

[英]Error while mapping MySQL Geometry to EJB entity

I need to add geospatial information, "Points" to be specific, to entries of a MySQL database (v5.5.28). 我需要向MySQL数据库(v5.5.28)的条目添加地理空间信息,即“点”。 I tried to map the created column "location" to the corresponding property of my java EJB entity ServiceInfo, using the simple code: 我尝试使用简单的代码将创建的列“位置”映射到我的Java EJB实体ServiceInfo的相应属性:

@Column(name = "location")
private com.vividsolutions.jts.geom.Point location;

However, this causes the following error: 但是,这导致以下错误:

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)
...

I think the problem is the java mysql connector (v5.1.22) doesn't support geospatial information, which surprises me since the database does. 我认为问题是Java mysql连接器(v5.1.22)不支持地理空间信息,这使我感到惊讶,因为数据库支持。 Can someone confirm this is the case, or tell me what I might be doing wrong and point me in the right direction? 有人可以确认是这种情况,还是告诉我我可能做错了什么,并指出正确的方向?

I'm working in Netbeans 7.2, using JDK 1.7 and a GlassFish Server 3.1.2 我正在使用JDK 1.7和GlassFish Server 3.1.2在Netbeans 7.2中工作

Ok, so I finally fixed my problem by making the following changes to my persistence tier: 好的,所以我最后通过对持久层进行以下更改来解决我的问题:

I switched from a MySQL database to a PostgeSQL database, using the spatial extension PostGIS. 我使用空间扩展PostGIS从MySQL数据库切换到PostgeSQL数据库。 The reason for this is that MySQL only partially implements the OpenGIS specifications. 原因是MySQL仅部分实现了OpenGIS规范。 Next, I had to change the persistence unit of EclipseLink (JPA 2.0)(Default) to Hibernate (JPA 2.0). 接下来,我必须将EclipseLink(JPA 2.0)(默认)的持久性单元更改为Hibernate(JPA 2.0)。

Jars to include in the classpath of both my project and glassfish (which seemed to be unable to find them otherwise. I had to copy them to the root of domain1) are: 我的项目和glassfish的类路径中都包含的jar(似乎无法找到它们。我不得不将它们复制到domain1的根目录):

  • postgresql-9.2-1002.jdbc4.jar 的postgresql-9.2-1002.jdbc4.jar
  • postgis-jdbc-2.1.0SVN.jar postgis-jdbc-2.1.0SVN.jar
  • Hibernate 4.1.8 (all required jars, ehcache for the optional ones and hibernate-entitymanager-4.1.8.Final.jar for JPA... These are all included in the release bundle) 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 and hibernate-spatial-postgis-1.1.1.jar are NOT compatible with Hibernate 4.x!). 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 jts-1.8.jar

The resulting persistence.xml file is: 生成的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>

The annotations to use for my EJB entitiy are: 用于我的EJB实体的注释是:

@Type(type="org.hibernate.spatial.GeometryType") 
@Column(name = "location", columnDefinition="Geometry")
private com.vividsolutions.jts.geom.Geometry location;

We now have a proper mapping using Hibernate + JPA + PostgeSQL/PostGIS. 现在,我们可以使用Hibernate + JPA + PostgeSQL / PostGIS进行适当的映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM