How to match duration type with JPA

I have a property field in a class that has a type javax.xml.datatype.Duration. It basically represents a time interval (e.g. 4 hours and 34 minutes).

JPA tells me that this is an invalid type that does not shock me.

What a good solution? I could implement my own Duration class, but I donโ€™t know how to force the JPA to โ€œacceptโ€ it as a data type.

+3
source share
3 answers

What a good solution? I could implement my own Duration class, but I donโ€™t know how to force the JPA to โ€œacceptโ€ it as a data type.

JPA , , , JPA . , Hibernate , @Type. , , . , , .

JPA getter/setter, . Long :

public MyEntity implements Serializable {
    private Long id;
    private javax.xml.datatype.Duration duration;

    @Id
    @GeneratedValue
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @Transient
    public Duration getDuration() {
        return this.duration;
    }
    public void setDuration(Duration duration) {
        this.duration = duration;
    }

    public Long getDurationAsJpaCompatibleType() {
        return MyXmlUtil.convertDurationToLong(this.duration);
    }
    public void setDurationAsJpaCompatibleType(Long duration) {
        setDuration(MyXmlUtil.convertLongToDuration(duration));
    }
}
+7

Duration , ... , / .

, , , @Embeddable JPA ( , ints).

, , @Embedded getter, , . @AttributeOverride.

+1

joda-time hibernate. , . joda Duration :

@Type(type="org.joda.time.contrib.hibernate.PersistentDuration")
private Duration startDateTime;
+1

Source: https://habr.com/ru/post/1745351/


All Articles