问题
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 propertyspring.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:
- Is there a way to get hibernate working with
org.joda.time.Interval
? - What is the preferred solution to migrate from
org.joda.time.Interval
asjava.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