【问题标题】:Migration from org.joda.time.Interval in Spring Boot从 Spring Boot 中的 org.joda.time.Interval 迁移
【发布时间】:2023-03-20 06:50:02
【问题描述】:

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

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

  • 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. 有没有办法让 org.joda.time.Interval 使用休眠模式?
  2. 由于java.time 没有类似的间隔类,从org.joda.time.Interval 迁移的首选解决方案是什么?

【问题讨论】:

    标签: java hibernate spring-boot jodatime java-time


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 2020-11-18
      • 2022-01-25
      • 1970-01-01
      • 2022-10-18
      • 2020-07-07
      • 2019-05-24
      • 1970-01-01
      • 2019-03-24
      相关资源
      最近更新 更多