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.