When injecting a PersistenceContext or PersistenceUnit, use EntityManager or
EntityManagerFactory objects, and do not use subclasses. This rule detects the presence
of injected subclasses of EntityManager or EntityManagerFactory.
The following conditions will trigger the rule:
- PersistenceContext:
- The annotation is defined on the field level.
- The field type is not EntityManager,
javax.persistence.EntityManager
- PersistenceUnit:
- The annotation is defined on the field level.
- The field type is not EntityManagerFactory,
javax.persistence.EntityManagerFactory
An automatic fix will replace those subclasses with EntityManager or EntityManagerFactory classes.
Examples:
- The rule detects this PersistenceContext code:
@PersistenceContext
CustomEntityManager myMgr;
- The automatic fix will make changes this code to:
@PersistenceContext
EntityManager
myMgr;
- Note: If the class does not already contain import statement of
javax.persistence.EntityManager
then your updated code will be similar to:
@PersistenceContext
javax.persistence.EntityManager
myMgr;
- The rule detects this PersitenceUnit code:
@PersistenceUnit
CustomEntityManagerFactory
myFactory;
- The automatic fix will make changes this code to:
@PersistenceUnit
EntityManagerFactory
myFactory;
- Note: If the class does not already contain import statement of
javax.persistence.EntityManagerFactory
then the updated code will be similar to:
@PersistenceUnit
javax.persistence.EntityManagerFactory
myFactory;