在Spring Boot中从org.joda.time.Interval迁移 [英] Migration from org.joda.time.Interval in Spring Boot

查看:111
本文介绍了在Spring Boot中从org.joda.time.Interval迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经使用org.joda.time.Interval表示具有固定开始和结束时间(不同于与特定时间无关的持续时间)的时间间隔,以便通过REST交换并在Spring Boot服务器应用程序中存储能量调度(2.2). 2.RELEASE).

I used to apply org.joda.time.Interval to represent a time interval with fixed start and end times (different from a duration which is independent from specific times) to exchange via REST and store energy schedules in a Spring Boot server application (2.2.2.RELEASE).

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

I tried different ways to store a org.joda.time.Interval field of an object via JPA/Hibernate:

  • jadira(7.0.0.CR1),其注释位于字段定义(@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentInterval"))上方
  • 设置了属性spring.jpa.properties.jadira.usertype.autoRegisterUserTypes=true
  • jadira(7.0.0.CR1)
  • jadira (7.0.0.CR1) with annotation above the field definition (@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentInterval"))
  • jadira (7.0.0.CR1) with property spring.jpa.properties.jadira.usertype.autoRegisterUserTypes=true set

但是,我总是得到

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

问题:

  1. 有没有办法让org.joda.time.Interval进入休眠状态?
  2. org.joda.time.Interval迁移的首选解决方案是什么,因为java.time没有类似的间隔等级?
  1. Is there a way to get hibernate working with org.joda.time.Interval?
  2. What is the preferred solution to migrate from org.joda.time.Interval as java.time does not have a similar interval class?

推荐答案

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

I ended up writing a custom class:

@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();
    }
}

这篇关于在Spring Boot中从org.joda.time.Interval迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆