| Since JPA 2.2, AttributeConverter-s should support CDI injection (see also HHH-12135 Closed ). This doesn't seem to work when I rely on the converter being auto-applied, like this:
@Converter(autoApply = true)
public class MyAttributeConverter implements AttributeConverter<MyAttribute, String> {
@Inject
private MyUtils utils;
...
}
@Entity
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private MyAttribute myAttribute;
...
}
This is basically Example 2: Auto-apply conversion of a basic attribute from the javadoc of @javax.persistence.Convert, so there should be nothing wrong with it. Yet, the utils field in MyAttributeConverter is null when the converter is invoked. However, if I apply the converter explicitly:
@Convert(converter = MyAttributeConverter.class)
private MyAttribute myAttribute;
then it works just fine. I'm no expert on JPA or CDI, but I believe auto-applied converters should support CDI as well. As a reproducer, I'm attaching a simple Maven project which produces a WAR. I then deploy the WAR into WildFly 18.0.0.Final, which includes Hibernate ORM 5.3.12.Final (a reasonably up-to-date version). |