无法使用Hibernate Spatial将Point插入mysql数据库

时间:2019-07-08 01:02:04

标签: java hibernate maven

我正在从docker镜像mysql:8.0.16使用mysql服务器。它具有MySQL InnoDB版本8.0.16

我正在尝试使用hibernate框架将具有Point变量的实体插入数据库。我总是收到相同的错误:

Cannot get geometry object from data you send to the GEOMETRY field

当我尝试向数据库中插入Booking类型时出现此错误(下面是Booking的定义)。预订有2个类型为Point的字段,由@Column注释,其列定义等于geometry(POINT, 0)

我正在使用数据库优先方法。 Hibernate不会自行创建任何表。

我正在使用com.vividsolutions.jts.geom.Point存储我的2分。

我调查了hibernate正在尝试执行此sql:

insert into Bookings (CarId, DropPosition, EndDateTime, PickupPosition, StartDateTime, UserId) values (13, x'ACED000573720021636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E506F696E7444077BAD161CBB2A0200014C000B636F6F7264696E617465737400304C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F436F6F7264696E61746553657175656E63653B78720024636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E47656F6D65747279799EA46522854A3E020004490004535249444C0008656E76656C6F70657400264C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F456E76656C6F70653B4C0007666163746F727974002D4C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F47656F6D65747279466163746F72793B4C000875736572446174617400124C6A6176612F6C616E672F4F626A6563743B787000000000707372002B636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E47656F6D65747279466163746F7279A158A0DFAE114CED020003490004535249444C0019636F6F7264696E61746553657175656E6365466163746F72797400374C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F436F6F7264696E61746553657175656E6365466163746F72793B4C000E707265636973696F6E4D6F64656C74002C4C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F507265636973696F6E4D6F64656C3B7870000000007372003F636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E696D706C2E436F6F7264696E617465417272617953657175656E6365466163746F7279C71B60593090D15702000078707372002A636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E507265636973696F6E4D6F64656C6BEE6404E9A25C3B0200024400057363616C654C00096D6F64656C547970657400314C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F507265636973696F6E4D6F64656C24547970653B787000000000000000007372002F636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E507265636973696F6E4D6F64656C2454797065B3467532BF594D420200014C00046E616D657400124C6A6176612F6C616E672F537472696E673B7870740008464C4F4154494E477073720038636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E696D706C2E436F6F7264696E617465417272617953657175656E6365F34BB588724E71F602000249000964696D656E73696F6E5B000B636F6F7264696E617465737400295B4C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F436F6F7264696E6174653B787000000003757200295B4C636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E436F6F7264696E6174653BEDC4D448AAFA43B702000078700000000173720026636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E436F6F7264696E6174655CBF2C235C7E583E02000344000178440001794400017A7870000000000000000000000000000000007FF8000000000000', '2019-07-08 00:46:57.809', x'ACED000573720021636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E506F696E7444077BAD161CBB2A0200014C000B636F6F7264696E617465737400304C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F436F6F7264696E61746553657175656E63653B78720024636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E47656F6D65747279799EA46522854A3E020004490004535249444C0008656E76656C6F70657400264C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F456E76656C6F70653B4C0007666163746F727974002D4C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F47656F6D65747279466163746F72793B4C000875736572446174617400124C6A6176612F6C616E672F4F626A6563743B787000000000707372002B636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E47656F6D65747279466163746F7279A158A0DFAE114CED020003490004535249444C0019636F6F7264696E61746553657175656E6365466163746F72797400374C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F436F6F7264696E61746553657175656E6365466163746F72793B4C000E707265636973696F6E4D6F64656C74002C4C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F507265636973696F6E4D6F64656C3B7870000000007372003F636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E696D706C2E436F6F7264696E617465417272617953657175656E6365466163746F7279C71B60593090D15702000078707372002A636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E507265636973696F6E4D6F64656C6BEE6404E9A25C3B0200024400057363616C654C00096D6F64656C547970657400314C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F507265636973696F6E4D6F64656C24547970653B787000000000000000007372002F636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E507265636973696F6E4D6F64656C2454797065B3467532BF594D420200014C00046E616D657400124C6A6176612F6C616E672F537472696E673B7870740008464C4F4154494E477073720038636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E696D706C2E436F6F7264696E617465417272617953657175656E6365F34BB588724E71F602000249000964696D656E73696F6E5B000B636F6F7264696E617465737400295B4C636F6D2F7669766964736F6C7574696F6E732F6A74732F67656F6D2F436F6F7264696E6174653B787000000003757200295B4C636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E436F6F7264696E6174653BEDC4D448AAFA43B702000078700000000173720026636F6D2E7669766964736F6C7574696F6E732E6A74732E67656F6D2E436F6F7264696E6174655CBF2C235C7E583E02000344000178440001794400017A7870000000000000000000000000000000007FF8000000000000', '2019-07-08 00:46:57.809', 1)]

我感觉到插入到点中的数据太大。我不确定是否在数据库中插入了正确的东西。

我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>someName</groupId>
    <artifactId>someName</artifactId>
    <version>0.1</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- Define Spring version as a constant -->
        <spring.version>5.1.8.RELEASE</spring.version>
        <hibernate.version>5.4.3.Final</hibernate.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.26</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.5.0</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>
        <!-- https://hibernate.org/validator/documentation/getting-started/ -->
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.16.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.vividsolutions</groupId>
            <artifactId>jts</artifactId>
            <version>1.13</version>
        </dependency>

    </dependencies>    
</project>

我的休眠配置文件:

<?xml version='1.0' encoding='utf-8'?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://127.0.0.1:11000/db</property>
        <property name="connection.username">user</property>
        <property name="connection.password">!Qazxsw2</property>


        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <property name="hibernate.dialect">org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">validate</property>

        <!-- Names the annotated entity class -->


    </session-factory>

</hibernate-configuration>

为预订创建脚本:

CREATE TABLE Bookings (
-- removed other fields for clarity
    PickupPosition POINT NOT NULL,
    DropPosition POINT,

)

Booking.java

@Entity
@Table( name = "Bookings" )
public class Booking {
    @Column(name = "PickupPosition", nullable = false, columnDefinition = "geometry(POINT, 0)")
    @NotNull
    private Point pickupPosition;


    @Column(name = "DropPosition", columnDefinition = "geometry(POINT, 0)")
    private Point dropPosition;

// removed other fields/methods/getters/setters for clarity
}

我尝试过的事情:

  • 设置不同的MySQL空间方言
    • 没有帮助
  • 从列注释中删除columnDefinition
    • 我收到以下错误Schema-validation: wrong column type encountered in column [DropPosition] in table [Bookings]; found [geometry (Types#BINARY)], but expecting [tinyblob (Types#VARBINARY)]
  • 在MySQL脚本中将类型从Point更改为Geometry
    • 没有帮助-相同的错误
  • 在Java代码中将类型从Point更改为Geometry
    • 没有帮助-相同的错误
  • 试图将ColumnDefinition更改为Point
    • 在构建sessionFactory Schema-validation: wrong column type encountered in column [DropPosition] in table [Bookings]; found [geometry (Types#BINARY)], but expecting [point (Types#VARBINARY)]期间我收到此错误

-

我读到了大量有关此问题的堆栈溢出问题,但是我没有发现任何与我完全相同的问题。

我只想将Point插入数据库。就是这样。

1 个答案:

答案 0 :(得分:0)

事实证明,解决我的问题的方法是使用org.locationtech.jts.geom.Point;而不是com.vividsolutions.jts.geom.Point