users@jpa-spec.java.net

[jpa-spec users] JPA_SPEC-109: Allow AttributeConverters to be CDI injectable

From: Lukas Jungmann <lukas.jungmann_at_oracle.com>
Date: Wed, 26 Apr 2017 20:23:47 +0200

Hi,

In JPA 2.1, injection support has been added to instances of
EntityListeners. Another candidates for injection support are instances
of AttributeConverter[1] interface. I'd like to propose adding following
text to the section 3.8 Type Conversion of Basic Attributes. The text
itself is almost identical to the one in section 3.5.1 Entity Listeners
covering injection handling in listeners in the JPA 2.1 spec (with
s/Entity listener/Attribute converter/g applied).

/*3.8 *//*Type Conversion of Basic Attributes*/

...
/Attribute converter classes in Java EE environments support dependency
injection through the Contexts and//
//Dependency Injection API (CDI) when CDI is enabled. An attribute
converter class that makes use//
//of CDI injection may also define life-cycle 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.//
//
//The persistence provider is responsible for using the CDI SPI to
create instances of the attribute converter//
//class; to perform injection upon such instances; to invoke their
PostConstruct and PreDestroy//
//methods, if any; and to dispose of the attribute converter instances.//
//
//The persistence provider is only required to support CDI injection
into attribute converter in Java EE container//
//environments. If the CDI is not enabled, the persistence provider must
not invoke attribute converter//
//that depend upon CDI injection.//
//
//An attribute converter is a non-contextual object. In supporting
injection into attribute converter, the persistence//
//provider must behave as if it carries out the following steps
involving the use of the CDI SPI.//
//• Obtain a BeanManager instance.//
//• Create an AnnotatedType instance for the attribute converter class.//
//• Create an InjectionTarget instance for the annotated type.//
//• Create a CreationalContext.//
//• Instantiate the converter by calling the InjectionTarget produce
method.//
//• Inject the converter instance by calling the InjectionTarget inject
method on the//
//instance.//
//• Invoke the PostConstruct callback, if any, by calling the
InjectionTarget postConstruct//
//method on the instance.//
/

/When the converter instance is to be destroyed, the persistence
provider must behave as if it carries out the//
//following steps.//
//• Call the InjectionTarget preDestroy method on the instance.//
//• Call the InjectionTarget dispose method on the instance//
//• Call the CreationalContext release method.//
/

/Persistence providers may optimize the steps above, e.g. by avoiding
calls to the actual CDI SPI and//
//relying on container-specific interfaces instead, as long as the
outcome is the same.//
/

/Attribute converters that do not make use of CDI injection are
stateless. The life-cycle of such attribute converters//
//is unspecified./

Do you think this is fine, something should be corrected or is there a
reason why this should not be added to JPA 2.2 MR?

Thank you,
--lukas

[1]: https://java.net/jira/browse/JPA_SPEC-109