Spring Boot 3+Hibernate Spatial 6迁移:MultiLineString的@Type注解报错问题
问题描述
我正尝试迁移至Spring Boot 3(搭配hibernate-spatial:6.4.1.Final)。迁移前,我的@Entity类中用于描述存储MultiLineString几何类型列的代码如下:
@Column(name = "multilinestring") @Type(value = "org.locationtech.jts.geom.MultiLineString") private MultiLineString multiLineString;
我直接将@Type改为@Type(org.locationtech.jts.geom.MultiLineString.class),但出现错误:MultiLineString未继承UserType(错误提示:incompatible types: Class cannot be converted to Class<? extends UserType<?>>)。
请问这种情况下我应该使用哪个类?或者我哪里操作有误?
以下是我的Gradle文件片段,供参考:
plugins { id 'org.springframework.boot' version '3.2.1' id 'io.spring.dependency-management' version '1.1.4' id 'java' } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-rest' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-mail' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.hibernate.orm:hibernate-spatial:6.4.1.Final' implementation 'net.postgis:postgis-jdbc:2023.1.0' implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6' .... }
解决方案
在Hibernate Spatial 6.x版本中,JTS几何类型的映射方式已经变更,不再需要手动指定@Type注解,Hibernate会自动识别JTS的几何类型并匹配对应的类型处理器。
你可以直接移除@Type注解,只保留@Column注解即可:
@Column(name = "multilinestring") private MultiLineString multiLineString;
如果因为某些原因必须显式指定类型,应该使用Hibernate Spatial提供的对应类型类,比如MultiLineStringType:
import org.hibernate.spatial.MultiLineStringType; @Column(name = "multilinestring") @Type(MultiLineStringType.class) private MultiLineString multiLineString;
额外注意事项:
- 确保数据库连接URL配置了对应PostGIS方言,例如
spring.jpa.properties.hibernate.dialect=org.hibernate.spatial.dialect.postgis.PostgisPG10Dialect,可根据你的PostgreSQL版本调整 - Hibernate Spatial 6.x已内置JTS类型支持,无需额外配置自定义UserType
内容的提问来源于stack exchange,提问作者Folli
相关产品推荐
相关产品推荐

