This rule detects certain JPA PeristenceContext or PersistenceUnit annotations where the
field name is the same as a persistence unit name that is defined in the persistence.xml file.
This rule might require access to the persistence.xml file, which must be located
in the META-INF folder. The following conditions trigger the rule:
- Marker Annotations:
- The annotation is defined on the field level.
- The field name is the same as a persistence unit name that is defined in the persistence.xml file.
- Normal Annotations:
- The annotation does not contain the attribute,
name
- The annotation contains
unitName
,
but does not contain
name
- The annotation does not contain "name", or "unitName", and is declared on Field and
the field name is the same as a persistence unit name defined in the
persistence.xml file
The automatic fix will add the attribute
unitName
and/or
name
to JPA PeristenceContext or PersistenceUnit annotations.
Examples:
- PersistenceContext
@PersistenceContext
EntityManager myPersistenceContext;
- If myPersistenceUnit is defined as a persistenceUnit in the
META-INF/persistence.xml file, the automatic fix will change this code to:
@PersistenceContext(unitName = "myPersistenceUnit", name = "myPersistenceUnit" )
EntityManager
myPersistenceContext;
- PersistenceUnit
@PersistenceUnit
EntityManagerFactory
myPersistenceUnit;
- If myPersistenceUnit is defined as a persistenceUnit in the
META-INF/persistence.xml file, the automated fix will change this code to:
@PersistenceUnit(unitName = "myPersistenceUnit", name = "myPersistenceUnit" )
EntityManagerFactory
myPersistenceUnit;
- PersistenceUnit
@PersistenceUnit(unitName="someName")
EntityManager
myPersistenceUnit;
- The automated fix will change this code to:
@PersistenceUnit(unitName = "someName", name = "someName" )
EntityManager
myPersistenceUnit;