gpt4 book ai didi

java - 无法使用 Hibernate Spatial 将点插入 mysql 数据库

转载 作者:行者123 更新时间:2023-12-02 03:20:54 27 4
gpt4 key购买 nike

我正在使用 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 的定义如下)。 Bookings 有 2 个 Point 类型的字段,由 @Column 注释,ColumnDefinition 等于 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>

我的 hibernate 配置文件:

<?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,

)

预订.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
    • 我收到以下错误架构验证:表 [Bookings] 中的 [DropPosition] 列中遇到错误的列类型;找到了 [geometry (Types#BINARY)],但期望 [tinyblob (Types#VARBINARY)]
  • 在 MySQL 脚本中将类型从 Point 更改为 Geometry
    • 没有帮助 - 同样的错误
  • 在 Java 代码中将类型从 Point 更改为 Geometry
    • 没有帮助 - 同样的错误
  • 尝试将 ColumnDefinition 更改为 Point
    • 我在构建 sessionFactory 期间收到此错误架构验证:表 [Bookings] 中的 [DropPosition] 列中遇到错误的列类型;找到了 [geometry (Types#BINARY)],但期望 [point (Types#VARBINARY)]

--

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

我只想将点插入数据库。仅此而已。

最佳答案

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

关于java - 无法使用 Hibernate Spatial 将点插入 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927248/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com