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

柔情痞子 提交于 2020-07-10 06:34:09

问题


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).

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

  • 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

However, I always get

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

Questions:

  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?

回答1:


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

and an attribute converter:

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


来源:https://stackoverflow.com/questions/60296211/migration-from-org-joda-time-interval-in-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!