tyler tyler - 7 months ago 38
Java Question

jpa independent custom type mapping / javax.persistence.x alternative to org.hibernate.annotations.Type and org.hibernate.annotations.TypeDef

I have a table

in a db that holds a column
of type
. The values in this column are 8-digit numbers representing an inverse date like '
'. Mapped onto this table i have a class
that holds a protected field iDate of type
. That field is annotated '
@Type(type = "inverseDate")
', using a custom type mapping. The class
is annotated with '
@TypeDef(name = "inverseDate", typeClass = InverseDateType.class)

import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

@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
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.