java jpa 独立自定义类型映射 / javax.persistence.x 替代 org.hibernate.annotations.Type 和 org.hibernate.annotations.TypeDef
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15973361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jpa independent custom type mapping / javax.persistence.x alternative to org.hibernate.annotations.Type and org.hibernate.annotations.TypeDef
提问by tyler
I have a table GameCycle
in a db that holds a column date
of type number
. The values in this column are 8-digit numbers representing an inverse date like '20130301
'. Mapped onto this table i have a class GameCycle
that holds a protected field iDate of type java.util.Date
. That field is annotated '@Type(type = "inverseDate")
', using a custom type mapping. The class Gamecycle
is annotated with '@TypeDef(name = "inverseDate", typeClass = InverseDateType.class)
'
我GameCycle
在数据库中有一个表,其中包含date
类型为 的列number
。此列中的值是 8 位数字,表示倒转日期,如“ 20130301
”。映射到这个表上,我有一个类GameCycle
,其中包含一个受保护的字段 iDate 类型java.util.Date
。该字段@Type(type = "inverseDate")
使用自定义类型映射注释为“ ”。该类Gamecycle
用“ @TypeDef(name = "inverseDate", typeClass = InverseDateType.class)
”注释
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
@Entity
@TypeDef(name = "inverseDate", typeClass = InverseDateType.class)
@Table(name = "GAMECYCLE")
public class GameCycle implements Comparable<GameCycle>, Serializable
{
@Type(type = "inverseDate")
@Column(name = "GC_DATE", nullable = false)
protected Date iDate = null;
...
Obviously, the imports bind me to using hibernate as a jpa implementation so my question is:
显然,导入将我绑定到使用 hibernate 作为 jpa 实现,所以我的问题是:
Is there a way to get rid of the hibernate annotations and do the same custom type mapping using a pure javax.persistence
solution ?
有没有办法摆脱休眠注释并使用纯javax.persistence
解决方案进行相同的自定义类型映射?
采纳答案by axtavt
No. Current version of JPA specification doesn't support custom type mappings. It's one of the most wanted features for future JPA 2.1.
不可以。当前版本的 JPA 规范不支持自定义类型映射。这是未来 JPA 2.1 最需要的功能之一。
If you really want to get rid of Hibernate-specific annoations, the only thing you can do is to map your field as String
and perform necessary conversion manually (in getters/setters).
如果您真的想摆脱特定于 Hibernate 的注释,您唯一能做的就是将您的字段映射为String
并手动执行必要的转换(在 getter/setter 中)。
But in practice almost every large JPA-based application uses some implementation-specific features of persistence provider, therefore I don't think that avoiding dependency on Hibernate in this case really matters.
但实际上,几乎每个基于 JPA 的大型应用程序都使用持久性提供程序的一些特定于实现的功能,因此我认为在这种情况下避免对 Hibernate 的依赖真的很重要。
回答by Guillaume Husta
Custom Type Mappinghas been finally added in JPA 2.1(JSR-388, part of Java EE 7).
The Hibernate's @Typeannotation is no longer needed, and can be replaced by Type Conversionin JPA 2.1.
JPA 2.1(JSR-388,Java EE 7 的一部分)中终于添加了自定义类型映射。不再需要
Hibernate 的@Type注解,可以用JPA 2.1 中的Type Conversion代替。
JPA 2.1 has added :
JPA 2.1 添加了:
- annotation @Convert
- annotation @Converts
- annotation @Converter
- interface AttributeConverter
- 注释@Convert
- 注释@Converts
- 注释@Converter
- 接口属性转换器
The most basic example : (Example 1: Convert a basic attribute) - from source
最基础的例子:(例子1:转换一个基本属性)——来自源码
@Converter
public class BooleanToIntegerConverter
implements AttributeConverter<Boolean, Integer>
{ ... }
...
...
@Entity
@Table(name = "EMPLOYEE")
public class Employee
{
@Id
private Long id;
@Column
@Convert(converter = BooleanToIntegerConverter.class)
private boolean fullTime;
}
Other links :
其他链接: