You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate 6.4.X是否支持Joda DateTime?Spring Boot升级编译问题求解

Spring Boot 3.2(Hibernate 6.4)迁移Joda DateTime问题解决

Hibernate 6.4.X不原生支持Joda DateTime,且原有的org.jadira.usertype库已不再适配Hibernate 6的SPI,因此你会遇到无法解析@Type注解的编译错误。

以下是两种可行的迁移方案:

方案一:替换为Java 8+ 标准时间API(推荐)

Spring Boot 3.x基于Java 17,建议直接迁移到Java官方的时间类(java.time包下),Hibernate 6原生支持这些类型,无需额外依赖。

修改后的实体类代码:

@jakarta.persistence.Entity
public class Order {
    // 替换为LocalDateTime(无时区)
    private LocalDateTime createdOn;

    // 若需时区支持,可使用ZonedDateTime
    // private ZonedDateTime createdOn;
}

Hibernate会自动将这些类型映射到数据库的TIMESTAMP类型,这是最简洁且符合现代Java开发趋势的方案。

方案二:自定义Hibernate Type保留Joda DateTime(仅适用于无法替换Joda的场景)

如果业务依赖无法移除Joda Time,需要自定义Hibernate类型来处理DateTime:

  1. 确保项目中引入Joda Time依赖:
<!-- Maven示例 -->
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.12.5</version>
</dependency>
  1. 实现自定义Hibernate BasicType:
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.jdbc.TimestampJdbcType;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

public class JodaDateTimeType extends AbstractSingleColumnStandardBasicType<DateTime> {
    public static final JodaDateTimeType INSTANCE = new JodaDateTimeType();
    private static final DateTimeFormatter FORMATTER = ISODateTimeFormat.dateTime();

    public JodaDateTimeType() {
        super(TimestampJdbcType.INSTANCE, (JavaType<DateTime>) (JavaType<?>) new DateTimeJavaType());
    }

    @Override
    public String getName() {
        return "joda_datetime";
    }

    private static class DateTimeJavaType implements JavaType<DateTime> {
        @Override
        public Class<DateTime> getJavaTypeClass() {
            return DateTime.class;
        }

        @Override
        public DateTime fromString(CharSequence string) {
            return FORMATTER.parseDateTime(string.toString());
        }

        @Override
        public String toString(DateTime value) {
            return FORMATTER.print(value);
        }

        // 需实现JavaType接口的其他必要方法,可参考Hibernate内置LocalDateTimeJavaType的实现逻辑
    }
}
  1. 注册自定义类型:
    创建TypeContributor类:
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.spi.TypeContributor;

public class JodaDateTimeTypeContributor implements TypeContributor {
    @Override
    public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
        typeContributions.contributeType(JodaDateTimeType.INSTANCE);
    }
}

在application.properties中配置类型贡献者:

hibernate.type_contributors=com.yourpackage.JodaDateTimeTypeContributor
  1. 在实体类中使用自定义类型:
@jakarta.persistence.Entity
public class Order {
    @Type(JodaDateTimeType.class)
    private DateTime createdOn;
}

注意:这种方式需要自行维护类型转换逻辑,后续Hibernate版本更新可能需要适配,仅建议在无法替换Joda的场景下使用。

内容的提问来源于stack exchange,提问作者Masudul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:34:58