Search code examples
javajpaeclipselinkconverters

EclipseLink global `@Converter`


Thanks to this thread, I was able to register and use a custom Converter for org.joda.time.DateTime using JPA EclipseLink. Here is a sample use (only the relevant parts):

@Converter(name = "jodaTimeConverter", converterClass = JodaDateTimeConverter.class)
public class MyEntity{

    @Column(name = "creationdate")
    @Temporal(TemporalType.TIMESTAMP)
    @Convert("jodaTimeConverter")
    private DateTime creationdate; 
}

I have many entity classes an most of them have a DateTime field. My question is thus: is it possible to register the converter once somewhere, so that all DateTime fields are automatically converted ?

I could obviously copy-paste the annotations everywhere, but a more DRY method would be appreciated.


Solution

  • What you're trying to use is a proprietary mechanism that would only work in EclipseLink, so leaving your code non-portable.

    A better option, if using JPA 2.1, is to make use of AttributeConverter, and set the converter itself to "autoApply". This means that it will be applied to all fields of the specified type without having to annotate every field. And with that you get portability too