在 Spring 引导中从 org.joda.time.Interval 迁移

Migration from org.joda.time.Interval in Spring Boot

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

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

然而,我总是得到

@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 没有类似的间隔 class?

我最终写了一个自定义 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();
    }
}