2

JPA 2.2 should support @Inject in AttributeConverter, as described in the specification:

Attribute converter classes in Java EE environments support dependency injection through the Contexts and Dependency Injection API (CDI) [ 7 ] when CDI is enabled[51]. An attribute converter class that makes use of CDI injection may also define lifecycle callback methods annotated with the PostConstruct and PreDestroy annotations. These methods will be invoked after injection has taken place and before the attribute converter instance is destroyed respectively.

But when I convert my JPA 2.1 conveter sample to JPA 2.2, it does not work.

The original Converter worked both Glassfish v4 and v5:

@Converter
public class ListToStringConveter implements AttributeConverter<List<String>, String> {

//@Inject Logger log;

@Override
public String convertToDatabaseColumn(List<String> attribute) {
    if (attribute == null || attribute.isEmpty()) {
        return "";
    }
    return StringUtils.join(attribute, ",");
}

@Override
public List<String> convertToEntityAttribute(String dbData) {
    if (dbData == null || dbData.trim().length() == 0) {
        return new ArrayList<String>();
    }

    String[] data = dbData.split(",");
    return Arrays.asList(data);
}
}

To taste the injection support of AttributeConverter in JPA 2.2, I extracted the conversion logic to anther CDI bean. And tried to run the codes in Glassfish v5(Java EE 8 Reference implementation).

@Converter(autoApply = false)
public class TagsConverter implements AttributeConverter<List<String>, String> {

//    private static final Logger LOG = Logger.getLogger(TagsConverter.class.getName());
//    
//     @Override
//    public String convertToDatabaseColumn(List<String> attribute) {
//        if (attribute == null || attribute.isEmpty()) {
//            return "";
//        }
//        return String.join( ",", attribute);
//    }
//
//    @Override
//    public List<String> convertToEntityAttribute(String dbData) {
//        if (dbData == null || dbData.trim().length() == 0) {
//            return new ArrayList<>();
//        }
//
//        String[] data = dbData.split(",");
//        return Arrays.asList(data);
//    }

    @Inject
    Logger LOG;

    @Inject
    ConverterUtils utils;

    @PostConstruct
    public void postConstruct(){
        LOG.log(Level.INFO, "calling @PostConstruct");
    }

    @PreDestroy
    public void preDestroy(){
        LOG.log(Level.INFO, "calling @PreDestroy");
    }

    @Override
    public String convertToDatabaseColumn(List<String> attribute) {
        LOG.log(Level.FINEST, "utils injected: {0}", utils != null);
        if (attribute == null || attribute.isEmpty()) {
            return "";
        }
        return utils.listToString(attribute);
    }

    @Override
    public List<String> convertToEntityAttribute(String dbData) {
        if (dbData == null || dbData.trim().length() == 0) {
            return Collections.<String>emptyList();
        }
        return utils.stringToList(dbData);
    }

}

And ConverterUtils class.

@ApplicationScoped
public class ConverterUtils {

    public String listToString(List<String> tags) {
        return join(",", tags);
    }

    public List stringToList(String str) {
        return Arrays.asList(str.split(","));
    }
}

In the TagsConverter, the expected ConverterUtils are not injected, and always get null when called it, a NPE threw.

The complete codes can be found here.

Update: I found I had created an issue on EclipseLink bugzilla 4 years ago.

MWiesner
  • 7,913
  • 11
  • 31
  • 66
Hantsy
  • 4,075
  • 3
  • 28
  • 61
  • There's no `beans.xml' to enable CDI? Which version of CDI? – Rouliboy Oct 19 '17 at 14:51
  • 1
    Why not mention which jpa 2.2 provider you are using? –  Oct 19 '17 at 15:25
  • @DN1 I have motioned I'v tested the codes in Glassfish v5, which shipped EclipseLink 2.7.0. – Hantsy Oct 20 '17 at 00:44
  • @Rouliboy Since Java EE 7 , there is no need to enable CDI by bean.xml,it is enabled by default(of course most of time I would like add a bean.xml explictly in my codes). I have motioned the original codes worked both Java EE 7/Java EE8. I have provided link of the completed codes. – Hantsy Oct 20 '17 at 00:46
  • @DN1 Any help here? – Hantsy Oct 21 '17 at 02:23

0 Answers0