Spring Boot 中从 org.joda.time.Interval 迁移

萨沙

我曾经申请org.joda.time.Interval表示具有固定开始和结束时间(不同于独立于特定时间的持续时间)的时间间隔,以通过 REST 进行交换并将能量计划存储在 Spring Boot 服务器应用程序 (2.2.2.RELEASE) 中。

我尝试了不同的方法来org.joda.time.Interval通过 JPA/Hibernate存储对象字段:

  • jadira(7.0.0.CR1) 在字段定义 ( @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentInterval"))上方带有注释
  • jadira(7.0.0.CR1) 与属性spring.jpa.properties.jadira.usertype.autoRegisterUserTypes=true

但是,我总是得到

@OneToOne or @ManyToOne on de.iwes.enavi.cim.schedule51.Schedule_MarketDocument.matching_Time_Period_timeInterval references an unknown entity: org.joda.time.Interval

问题:

  1. 有没有办法让 hibernate 工作org.joda.time.Interval
  2. org.joda.time.Interval由于java.time没有类似的区间类,迁移的首选解决方案是什么
萨沙

我最终编写了一个自定义类:

@Entity
public class FInterval {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private long id;

    @Column
    private long startMillis;

    @Column
    private long endMillis;

    public FInterval() {
    }

    public long getStartMillis() {
        return startMillis;
    }

    public void setStartMillis(long start) {
        this.startMillis = start;
    }

    public long getEndMillis() {
        return endMillis;
    }

    public void setEndMillis(long end) {
        this.endMillis = end;
    }

    public FInterval(Interval entity) {
            this.startMillis = entity.getStartMillis();
            this.endMillis = entity.getEndMillis();
        }

    public Interval getInterval() {
        return new Interval(this.startMillis,this.endMillis);
    }
}

和一个属性转换器:

@Converter(autoApply = true)
public class IntervalAttributeConverter implements AttributeConverter<Interval, FInterval> {

    @Override
    public FInterval convertToDatabaseColumn(Interval attribute) {
        return new FInterval(attribute);
    }

    @Override
    public Interval convertToEntityAttribute(FInterval dbData) {
        return dbData.getInterval();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

java.time中是否有与Joda-Time Interval相当的类?

Spring bean无法解析org.joda.time.DateTime的构造函数

Flyway可重复迁移-在Spring Boot中不执行

Joda Time / Jackson 2 / Spring的反序列化问题-无法实例化类型的值[简单类型,类org.joda.time.DateTime]

如何使用 Scala 在 Spark Streaming 中序列化 org.joda.time.DateTime?

在Java中8极品非proleptic公历纪年(如org.joda.time.chrono.GJChronology)

线程“主”中的异常java.lang.NoClassDefFoundError:org / joda / time / ReadableInstant

在Spring Boot JPA中的DATE TIME删除

将org.joda.time转换为java.time

从Joda Time到Java时间的迁移在迁移时显示出不同的结果

Spring Boot 2.3中未解决Org.Junit

在 Spring Boot 2.7.0 中找不到 org.junit.Test

应该将org.joda.time.DateTimeZone声明为类中的静态字段,而不是每次都创建新实例

Jodatime:无法解析导入的org.joda.time.Weeks

java.lang.NoClassDefFoundError:org.joda.time.DateTime

如何使用org.joda.time设置时区?

在AndroidX中导入org.joda.time的问题

object time不是包org.joda的成员

参数类型的隐式 ParameterBinderFactory[org.joda.time.LocalDateTime]

将Spring Boot 1.5.10迁移到Spring Boot 2.0.3

从Spring Boot Security迁移到Keycloak

Spring Boot Flyway迁移占位符

Spring Boot 2迁移依赖关系问题

在将Spring Boot项目迁移到Gradle Kotlin DSL中摆脱警告

Flyway迁移过程中是否需要停止Spring Boot应用程序?

从1.5到2.0的Spring Boot迁移,并在JPA存储库查询中的参数周围加上引号

SERVLET_MAPPING 后缀 .do 在 Struts1 中向 Spring Boot 迁移

月末在Joda Time月份中循环播放

从Spring Boot Oauth2迁移到Spring Security 5