jpa独立自定义类型映射/ javax.persistence.x替代org.hibernate.annotations.Type和org.hibernate.annotations.TypeDef [英] jpa independent custom type mapping / javax.persistence.x alternative to org.hibernate.annotations.Type and org.hibernate.annotations.TypeDef
问题描述
在包含类型的列
。此栏中的值为8位数字,代表反向日期,如' date
的数据库中有一个表 GameCycle
编号 20130301
'。映射到这张表上,我有一个类 GameCycle
,它包含一个类型为 java.util.Date
的受保护字段iDate。该字段使用自定义类型映射注释为' @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可比较的< GameCycle> ;,可序列化的
{
@Type(type =inverseDate)
@Column(name =GC_DATE,nullable = false)
保护日期iDate = null ;
...
很明显,导入让我使用hibernate作为jpa实现,所以我的问题是:
有没有办法摆脱hibernate注释并使用纯 javax.persistence
解决方案?
JPA规范的当前版本不支持自定义类型映射。这是未来JPA 2.1最受欢迎的功能之一。
如果你真的想摆脱Hibernate特有的注释,唯一能做的就是映射你的字段作为字符串
并且手动执行必要的转换(在getters / setters中)。然而在实践中,几乎每个大的JPA基于应用程序使用持久性提供程序的一些特定于实现的功能,因此我认为在这种情况下避免依赖Hibernate确实很重要。
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)
'
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:
Is there a way to get rid of the hibernate annotations and do the same custom type mapping using a pure javax.persistence
solution ?
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.
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).
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独立自定义类型映射/ javax.persistence.x替代org.hibernate.annotations.Type和org.hibernate.annotations.TypeDef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!