Registering Converters in JPA 2.1 with EclipseLink

前端 未结 4 1848
醉酒成梦
醉酒成梦 2021-02-06 16:00

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

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

4条回答
  •  梦谈多话
    2021-02-06 16:41

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

    Car entity

    Stays the same

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

    CarColor Converter

    You only need the empty Annotation

    import javax.persistence.AttributeConverter;
    import javax.persistence.Converter;
    
    @Converter
    public class CarColorConverter implements AttributeConverter {
        [...]
    }
    

    persistence.xml

    You can either

    • declare no class at all

    or

    • declare every class that is involved.

    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.

    
    
    
        
    
            
            com.xyz.model.converters.CarColorConverter
    
            
            com.xtz.model.Car
    
            [...]
        
    
    

提交回复
热议问题