【问题标题】:Hibernate-spatial jpa with myql and Spring boot 1.5.7 not working使用 mysql 和 Spring boot 1.5.7 的 Hibernate-spatial jpa 无法正常工作
【发布时间】:2018-03-20 00:47:56
【问题描述】:

我想在 mysql 和 spring boot 中使用休眠空间。我试过但失败了。 下面给出的 application.properties 文件

spring.datasource.url=jdbc:mysql://localhost:3306/tour_management
spring.datasource.username=root
spring.datasource.password=admin
endpoints.actuator.enabled=true
endpoints.info.enabled=true

spring.jpa.properties.hibernate.dialect = 
org.hibernate.spatial.dialect.mysql.MySQLSpatial5InnoDBDialect
spring.jpa.database-platform = 
org.hibernate.spatial.dialect.mysql.MySQLSpatial5InnoDBDialect



@Data
@Entity(name = "Place")
public class PlaceEntity extends BaseEntity {
@Id
@GeneratedValue
@Column(name = "ID")
private long id;
@Column(name = "NAME")
private String name;
@Column(name = "CODE")
private String code;
@Column(name = "LONGITUDE")
private Double longitude;
@Column(name = "LATITUDE")
private Double latitude;

@Column(name = "LOCATION",columnDefinition = "geometry(Point,4326)")
private Point location;
}

但部署时出现异常

buildscript {
ext {
    springBootVersion = '1.5.7.RELEASE'
}
repositories {
    mavenCentral()
    maven {
        url "http://www.hibernatespatial.org/repository"
    }
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-
plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.fm'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven {
    url "http://www.hibernatespatial.org/repository"
}
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('mysql:mysql-connector-java')
compile('org.projectlombok:lombok:1.14.8')
compile('org.springframework.boot:spring-boot-starter-web')
compile('com.vividsolutions:jts:1.13')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

原因:org.hibernate.boot.registry.selector.spi.StrategySelectionException:无法将名称 [org.hibernate.spatial.dialect.mysql.MySQLSpatial5InnoDBDialect] 解析为策略 [org.hibernate.dialect.Dialect] 在 org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:113) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]

有没有版本相关的问题???

【问题讨论】:

    标签: mysql spring spring-boot hibernate-spatial


    【解决方案1】:

    如果我们在运行时匹配您的异常:

    原因: org.hibernate.boot.registry.selector.spi.StrategySelectionException: 无法解析名称 [org.hibernate.spatial.dialect.mysql.MySQLSpatial5InnoDBDialect] 为 策略 [org.hibernate.dialect.Dialect] 在 org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:113) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]

    StrategySelectorImpl的源码:

    @Override
        @SuppressWarnings("unchecked")
        public <T> Class<? extends T> selectStrategyImplementor(Class<T> strategy, String name) {
            final Map<String,Class> namedStrategyImplementorMap = namedStrategyImplementorByStrategyMap.get( strategy );
            if ( namedStrategyImplementorMap != null ) {
                final Class registered = namedStrategyImplementorMap.get( name );
                if ( registered != null ) {
                    return (Class<T>) registered;
                }
            }
    
            try {
                return classLoaderService.classForName( name );
            }
            catch (ClassLoadingException e) {
                throw new StrategySelectionException(
                        "Unable to resolve name [" + name + "] as strategy [" + strategy.getName() + "]"
                );
            }
        }
    

    我们了解到org.hibernate.spatial.dialect.mysql.MySQLSpatial5InnoDBDialect 类不能被类加载器加载。它可能不在类路径中。

    我想你应该添加与你的 Hibernate 版本匹配的 hibernate 空间依赖。

    【讨论】:

    • 我添加了 hibernate-core、hibernate-spatial 和 hibernate-entitymanager jar。然后它部署但现在另一个问题,当我在 DB 上插入数据时,出现以下错误。 2017-10-09 03:14:52.343 WARN 6000 --- [nio-8080-exec-1] ohengine.jdbc.spi.SqlExceptionHelper:SQL 错误:1416,SQLState:22001 2017-10-09 03:14: 52.343 错误 6000 --- [nio-8080-exec-1] ohengine.jdbc.spi.SqlExceptionHelper:数据截断:无法从您发送到 GEOMETRY 字段的数据中获取几何对象
    • 这与最初的问题无关。如果它们不混合问题,它会使问题更可重用。请打开一个新问题并仅显示以下代码中的相关代码:插入数据库的代码/脚本和相关实体。
    猜你喜欢
    • 2020-03-26
    • 1970-01-01
    • 2015-04-20
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2020-02-09
    • 2019-01-23
    相关资源
    最近更新 更多