使用EclipseLink在JPA 2.1中注册转换器 [英] Registering Converters in JPA 2.1 with EclipseLink

查看:102
本文介绍了使用EclipseLink在JPA 2.1中注册转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaEE环境中,我使用EclipseLink的JPA 2.1实现,

On JavaEE environment, I use JPA 2.1 implementation with EclipseLink,

我有一些实体包含 enums 。所以我为这些枚举创建了转换器。

I have some entities that contain enums. So I have created converters for these enumerations.

汽车实体:

@Entity
public class Car implements Serializable {

    private static final long serialVersionUID = 6L;

    @Id
    private String      id;

    @Convert (converter = CarColorConverter.class)
    private CarColor    color;

    public enum CarColor {
        Black,
        Gray,
        White,
        Red
    };

    public Car () {
        id = GenerateUUID.id ();
    }

    ....
}

CarColor转换器:

@Converter (converterClass = CarColorConverter.class, name = "CarColorConverter") 
public class CarColorConverter implements AttributeConverter<CarColor, String> { 

    private static final String BLACK   = "Black";
    private static final String GRAY    = "Gray";
    private static final String WHITE   = "White";
    private static final String RED     = "Red";

    @Override
    public String convertToDatabaseColumn (CarColor entityData) {

        switch (entityData) {
            case Black:
                return BLACK;

            case Gray:
                return GRAY;

            case White:
                return WHITE;

            case Red:
                return RED;

            default:
                throw new IllegalArgumentException ("Unknown : " + entityData);
        }
    }

    @Override
    public CarColor convertToEntityAttribute (String dbData) {

        switch (dbData) {
            case BLACK:
                return CarColor.Black;

            case GRAY:
                return CarColor.Gray;

            case WHITE:
                return CarColor.White;

            case RED:
                return CarColor.Red;

            default:
                throw new IllegalArgumentException ("Unknown : " + dbData);
        }
    }
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence 
    version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="xyz-restful-api" transaction-type="RESOURCE_LOCAL">

        <!-- Converters -->
        <!--<class>com.xyz.model.converters.CarColorConverter</class>-->

        <!-- Entities / Model -->
        <class>com.xtz.model.Car</class>

        <properties>
            ...
        </properties>

    </persistence-unit>

</persistence>




  1. 我发表评论时在persistence.xml文件中声明转换器,并尝试在DB中保留我的实体我收到此错误: 请确保转换器类名称正确并且存在持久性单元定义。 。并且没有编译时异常,只有一个非常明确的警告:

  1. When I comment the declaration of the converter on the persistence.xml file, and try to persist my entities in the DB I get this error : "Please ensure the converter class name is correct and exists with the persistence unit definition.". and no compilation time exception, only a warning pretty explicit :


类com.xyz.model.converters.CarTypeConverter是注释的,但是未列在persistence.xml文件中

Class "com.xyz.model.converters.CarTypeConverter" is annotated, but not listed in the persistence.xml file


  • 但是,当我取消注释时,声明转换器在persistence.xml文件上,并尝试在DB中保留我的实体我收到此错误:请确保转换器类名称正确并且存在持久性单元定义。。和编译时间异常:

  • However, when I uncomment the declaration of the converter on the on the persistence.xml file, and try to persist my entities in the DB I get this error : "Please ensure the converter class name is correct and exists with the persistence unit definition.". and a compilation time exception :


    类com.xyz.model.converters.CarColorConverter列在persistence.xml文件中,但是没有注释

    Class "com.xyz.model.converters.CarColorConverter" is listed in the persistence.xml file, but is not annotated


  • 我是否以错误的方式声明转换器?

    Am I declaring the converters in a wrong way ?

    谢谢。

    推荐答案

    试一试,确保你已经包含正确的套餐。

    Give this a try and ensure you have included the correct packages.

    保持相同

    import javax.persistence.Convert;
    
    @Entity
    public class Car implements Serializable {
    
        [...]
    
        @Convert(converter = CarColorConverter.class)
        private CarColor    color;
    
        [...]
    }
    



    CarColor Converter



    您只需要空注释

    CarColor Converter

    You only need the empty Annotation

    import javax.persistence.AttributeConverter;
    import javax.persistence.Converter;
    
    @Converter
    public class CarColorConverter implements AttributeConverter<CarColor, String> {
        [...]
    }
    



    persistence.xml



    您可以


    • 根本不申报任何课程


    • 声明所涉及的每个类。

    一旦你需要手动声明一个实体(例如当它在库中抵制时) )那么你还需要声明所有其他实体/转换器类。

    As soon as you need to declare an entity manually (e.g. when it resists in a library) then you also need do declare all other entity/converter classes.

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence 
        version="2.1"
        xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    
        <persistence-unit name="xyz-restful-api" transaction-type="RESOURCE_LOCAL">
    
            <!-- Converters -->
            <class>com.xyz.model.converters.CarColorConverter</class>
    
            <!-- Entities / Model -->
            <class>com.xtz.model.Car</class>
    
            [...]
        </persistence-unit>
    </persistence>
    

    这篇关于使用EclipseLink在JPA 2.1中注册转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆