【问题标题】:Liquibase generate migration for geometry type (Spring boot with JPA/Hibernate)Liquibase 为几何类型生成迁移(使用 JPA/Hibernate 的 Spring Boot)
【发布时间】:2021-11-11 21:26:12
【问题描述】:

我正在编写一个带有 Spring Boot 的 RESTfull API,使用 Maven Liquibase 来管理数据库的 MySQL 8 迁移。

我在网上搜索过(123),但 Liquibase 仍在迁移中生成“TINYBLOB”类型,而不是“POINT”或“GEOMETRY”。令人惊讶的是,当我编辑迁移文件(即 changeSet 并使用“POINT”时,mvn liquibase:update 仍会在数据库上创建一个TINYBLOB 列。

我有一个典型的 JPA 实体:

import org.locationtech.jts.geom.Point;


@Entity
class MyModel {

  private Point location;

  // more fields

我使用的是 Liquibase 4.3 版和 Hibernate 5.4 版。对于hibernate dialect,我使用的是org.hibernate.spatial.dialect.mysql.MySQL8SpatialDialect

在我看来,Liquibase 不假定空间类型......但这会令人惊讶。任何帮助将不胜感激(所有其他数据类型的行为都符合预期)。

【问题讨论】:

    标签: mysql spring-boot hibernate liquibase liquibase-hibernate


    【解决方案1】:

    前段时间遇到了同样的问题,最终手动覆盖了自动生成的迁移文件的部分内容。在 MySQL 8 上运行良好。

     <!--    Define the type-->
    <property name="pointType" value="geometry" dbms="h2"/> <!--    Only relevant for in-memory integration tests-->
    <property name="pointType" value="POINT" dbms="mysql, oracle, mssql, mariadb, postgresql"/>
    
    <!--    Use the type on the column-->
    <column name="location" type="${pointType}">
        <constraints nullable="true" />
    </column>
    

    我的 Hibernate 模型的简化版本。

    package com.stackoverflow.sample.domain;
    
    
    import com.vividsolutions.jts.geom.Point;
    import org.hibernate.annotations.Type;
    import org.springframework.data.elasticsearch.annotations.FieldType;
    
    import javax.persistence.*;
    import java.io.Serializable;
    
    
    @Entity
    @Table(name = "some_entity")
    public class SomeEntity implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @org.springframework.data.elasticsearch.annotations.Field(type = FieldType.Keyword)
        private Long id;
    
        @Type(type = "com.vividsolutions.jts.geom.Point")
        @Column(name = "location", nullable = false, columnDefinition = "geometry")
        private Point location;
    
    }
    

    【讨论】:

    • 对于其他人,如果您还没有将 H2GIS Functions 设置为 dependency,则需要添加它才能运行测试。另外,请确保为您的测试配置设置spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect
    猜你喜欢
    • 1970-01-01
    • 2019-01-22
    • 2020-01-28
    • 2017-06-17
    • 2018-08-14
    • 1970-01-01
    • 2017-09-29
    • 2017-09-25
    • 2017-02-11
    相关资源
    最近更新 更多