Grails 3.4.x迁移至4.1.0时Joda-Time映射问题求助
Grails 4.1.0迁移中Joda-Time映射失效及GC问题解决方案
问题根源
Grails 4升级了底层Hibernate到5.x版本,原适配Hibernate 4的Jadira UserType旧版本与Hibernate 5存在兼容性问题,导致全局默认映射配置触发无限初始化循环,最终引发GC内存超限。
修复步骤
1. 更新Jadira UserType依赖
替换为适配Hibernate 5的版本,在build.gradle中修改:
implementation 'org.jadira.usertype:usertype.core:6.0.1.GA'
执行
./gradlew dependencies排查依赖冲突,确保旧版本Jadira被彻底排除。
2. 修正GORM映射配置
Grails 4的GORM全局映射语法变更,原写法不再生效,改为以下两种方式:
方式一:全局统一配置(推荐)
在application.groovy中调整为:
grails.gorm.default.mapping = { all { userType(type: org.jadira.usertype.dateandtime.joda.PersistentDateTime, class: org.joda.time.DateTime) userType(type: org.jadira.usertype.dateandtime.joda.PersistentLocalDate, class: org.joda.time.LocalDate) userType(type: org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime, class: org.joda.time.LocalDateTime) } }
方式二:单个领域类显式配置
若全局配置仍有问题,在每个含Joda类型的领域类中单独配置:
class Order { DateTime orderTime LocalDate deliveryDate static mapping = { orderTime type: org.jadira.usertype.dateandtime.joda.PersistentDateTime deliveryDate type: org.jadira.usertype.dateandtime.joda.PersistentLocalDate } }
3. 优化JVM参数(可选)
临时缓解内存压力,添加JVM启动参数:
-Xmx4G -XX:+UseG1GC -XX:MaxGCPauseMillis=200
极简替代方案
使用Grails官方Joda-Time插件自动处理映射,无需手动配置:
implementation 'org.grails.plugins:joda-time:2.1.1'
插件会自动注册Joda类型的持久化映射,兼容Grails 4.x版本。
内容的提问来源于stack exchange,提问作者sufyan.shoaib
相关产品推荐
相关产品推荐

