Since JPA 2.2, {{AttributeConverter}}-s should support CDI injection (see also [ HHH-12135 ] ). This doesn't seem to work when I rely on the converter being auto-applied, like this:
{code:java} @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;
... } {code}
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:
{code:java} @Convert(converter = MyAttributeConverter.class) private MyAttribute myAttribute; {code}
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). |
|