gpt4 book ai didi

hibernate - 如何使用 JPA 持久保存 XMLGregorianCalendar?

转载 作者:行者123 更新时间:2023-12-02 23:36:48 25 4
gpt4 key购买 nike

这就是我所拥有的:

@XmlSchemaType(name = "dateTime")
@Column(name = "expiry-date", nullable = false)
protected XMLGregorianCalendar expiryDate;

抛出以下异常:

org.hibernate.MappingException: Could not determine type for: javax.xml.datatype.XMLGregorianCalendar

谢谢

最佳答案

JPA 不支持

XMLGregorianCalendar,您可以使用 java.util.Datejava.util.Calendar (解释 here )。

也许你可以看看这个项目 Hyperjaxb3 (为 JAXB 对象提供关系持久性)。 Here它解释了如何处理时间属性:

Temporal properties (typed xsd:dateTime, xsd:date, xsd:time and so on) will be mapped as temporal JPA properties. Hyperjaxb3 will choose temporal type as TIMESTAMP, DATE or TIME depending on the XML Schema type of the temporal property. Temporal properties are typically mapped onto XMLGregorianCalendar which is not supported by JPA - and therefor must be wrapped:

<xs:element name="dateTime" type="xs:dateTime" minOccurs="0"/>
<小时/>
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar dateTime;
@Transient
public XMLGregorianCalendar getDateTime() {
return dateTime;
}
public void setDateTime(XMLGregorianCalendar value) {
this.dateTime = value;
}
@Basic
@Column(name = "DATETIMEITEM")
@Temporal(TemporalType.TIMESTAMP)
public Date getDateTimeItem() {
return XmlAdapterUtils.unmarshall(XMLGregorianCalendarAsDateTime.class, this.getDateTime());
}

public void setDateTimeItem(Date target) {
setDateTime(XmlAdapterUtils.marshall(XMLGregorianCalendarAsDateTime.class, target));
}

另一种方法是编写您自己的解决方案,执行一些转换,而不是尝试保留您的 JAXB 对象。

关于hibernate - 如何使用 JPA 持久保存 XMLGregorianCalendar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28394134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com