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

前端 未结 2 1604
离开以前
离开以前 2020-12-09 18:52

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 a

相关标签:
2条回答
  • 2020-12-09 19:31

    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.

    0 讨论(0)
  • 2020-12-09 19:54

    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
    0 讨论(0)
提交回复
热议问题