Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

java - 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :

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 :


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...