Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 666
tyler Avatar asked Apr 12 '13 14:04

tyler


People also ask

Why we are using JPA annotation instead of hibernate?

You use hibernate as implementation of JPA API. You should be able to change hibernate with another implementation (like EclipseLink) without changing in the code. This is why you should only use JPA annotations.

What is the difference between ORM JPA and Hibernate?

Java Persistence API (JPA) defines the management of relational data in the Java applications. Hibernate is an Object-Relational Mapping (ORM) tool which is used to save the state of Java object into the database. It is just a specification. Various ORM tools implement it for data persistence.

Is javax persistence same as hibernate?

Hibernate is an implementation of the Java Persistence API, and where possible, you should use the standard annotations (in javax. persistence). This way, you could theoretically run your code on other JPA implementations. Only when you need Hibernate-specific functionality should you use the Hibernate annotations.


2 Answers

Custom Type Mapping has been finally added in JPA 2.1 (JSR-388, part of Java EE 7).
The Hibernate's @Type annotation is no longer needed, and can be replaced by Type Conversion in JPA 2.1.

JPA 2.1 has added :

  • annotation @Convert
  • annotation @Converts
  • annotation @Converter
  • interface AttributeConverter

The most basic example : (Example 1: Convert a basic attribute) - from source

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

  • Type Conversion in JPA 2.1
  • JPA 2.1 - How to implement a Type Converter
  • Mapping Enums Done Right With @Convert in JPA 2.1
like image 182
Guillaume Husta Avatar answered Sep 29 '22 22:09

Guillaume Husta


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.

like image 26
axtavt Avatar answered Oct 01 '22 22:10

axtavt