如何映射使用JPA持续时间类型 [英] How to map Duration type with JPA
问题描述
我有一个类,它是一个类型的属性字段 javax.xml.datatype.Duration中
。它基本上重新presents的时间跨度(例如,4小时34分钟)。
JPA是告诉我这是一个无效的类型,它不激我。
请告诉我一个很好的解决方案呢?我可以实现我自己的时间类,但我不知道怎么去JPA接受它作为一种数据类型。
请告诉我一个很好的解决方案呢?我可以实现我自己的时间类,但我不知道怎么去JPA接受它作为一种数据类型。
块引用>JPA不支持自定义类型,因此,如果你想要走这条路,你将不得不使用从您的提供商一个JPA扩展。例如,休眠允许定义<一href=\"http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-types-custom\">custom值类型中,你与<声明一href=\"http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e2794\"><$c$c>@Type$c$c>.显然,这将损害供应商这可能是一个问题之间的可移植性。如果没有,那么你知道它是可行的。
使用标准JPA,传统的方法是添加另一个的getter / setter一对适应问题的性质和访问时进行转换。我会用一个
龙
来存储时间:公共myEntity所实现Serializable {
私人长期身份证;
私人javax.xml.datatype.Duration中的持续时间; @ID
@GeneratedValue
众长的getId(){
返回this.id;
}
公共无效SETID(长ID){
this.id = ID;
} @短暂的
公开时间getDuration(){
返回this.duration;
}
公共无效setDuration(持续时间){
this.duration =持续时间;
} 众长getDurationAsJpaCompatibleType(){
返回MyXmlUtil.convertDurationToLong(this.duration);
}
公共无效setDurationAsJpaCompatibleType(持续时间长){
setDuration(MyXmlUtil.convertLongToDuration(持续时间));
}
}I have a property field in a class that is of type
javax.xml.datatype.Duration
. It basically represents a time span (e.g. 4 hours and 34 minutes).JPA is telling me it is an invalid type, which doesn't shock me.
Whats a good solution this? I could implement my own Duration class, but I don't know how to get JPA to "accept" it as a datatype.
解决方案Whats a good solution this? I could implement my own Duration class, but I don't know how to get JPA to "accept" it as a datatype.
JPA does not support custom types so if you want to go this way you'll have to use a JPA extension from your provider. For example, Hibernate allows to define custom value types that you declare with
@Type
. Obviously, this will harm portability between providers which might be a concern. If not, then you know it is doable.With standard JPA, the traditional approach would be to add another getter/setter pair which adapt the problematic property and perform conversion when accessed. I would use a
Long
to store a duration: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)); } }
这篇关于如何映射使用JPA持续时间类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!