Search code examples
javajpaearpersistence.xml

How to scan Entity in EJB module


I have an ear file with structure below. [ejb-1.jar] and [ejb-2.jar] is both contain EBJ bean and Entity class. However, when I try to deploy it to server, JPA does not scan my entity in ejb jar file. Can any one tell me how to config persistence.xml file?

ear

-- lib (folder)

-------- log4j.jar (ex)

-- ejb-1.jar

-- ejb-2.jar

-- app.war

-- meta-inf (folder)

-------- persisetence.xml


Solution

  • Place the persistence.xml in the META-INF inside each EJB jar file.

    Also you can uyse tag to point to specific jars holding entities.

    according to section 8.2 JPA specification:

    Within Java EE environments, an EJB-JAR, WAR, EAR, or application client JAR can define a persistence unit. Any number of persistence units may be defined within these scopes. A persistence unit may be packaged within one or more jar files contained within a WAR or EAR, as a set of classes within an EJB-JAR file or in the WAR classes directory, or as a combination of these as defined below. A persistence unit is defined by a persistence.xml file. The jar file or directory whose META-INF directory contains the persistence.xml file is termed the root of the persistence unit. In Java EE environments, the root of a persistence unit must be one of the following:

    • an EJB-JAR file
    • the WEB-INF/classes directory of a WAR file[80]
    • a jar file in the WEB-INF/lib directory of a WAR file
    • a jar file in the EAR library directory
    • an application client jar file It is not

      required that an EJB-JAR or WAR file containing a persistence unit be packaged in an EAR unless the persistence unit contains persistence classes in addition to those contained within the EJB-JAR or WAR. See Section 8.2.1.6.