Topic
  • 3 replies
  • Latest Post - ‏2011-07-13T15:41:28Z by leealber
infyRavi
infyRavi
14 Posts

Pinned topic Mentioning entity classes in Persistence.xml file

‏2011-07-12T12:18:50Z |
Hi ,
In Persistence.xml file ,now we are adding all the entity classes, but we don't want to do it like that.
Is it possible that we can put all the entity classes in a jar file and give that reference in persistence.xml file.
In that case how will my EAR structure should be ? do I need to package entity classes separately in a jar file and add it in
EAR/lib and specify the dependency in WEB-INF/MANIFEST.MF file.
How will persistence.xml file pickup these jar files?
Can you please throw some light on this aspect.,

Thanks
Ravi
Updated on 2011-07-13T15:41:28Z at 2011-07-13T15:41:28Z by leealber
  • leealber
    leealber
    19 Posts

    Re: Mentioning entity classes in Persistence.xml file

    ‏2011-07-12T14:45:00Z  
    Ravi,

    If you are deploying the application in JEE mode, you don't have to explicitly list the entity classes in the persistence unit. The container should search for the entities from the classpath. However JSE JPA application **MUST** enumerate the entities in the persistence unit using the <classes> element.

    One way to organize your application is to create a jar that packages all the entities and specify its location using the <jar-file> element in the persistence unit. See the examples in the 8.2.1.6.3 section below.

    Here are the related sections in the JPA spec.

    8.2.1.6.3 Jar Files
    One or more JAR files may be specified using the jar-file elements instead of, or in addition to the
    mapping files specified in the mapping-file elements. If specified, these JAR files will be searched
    for managed persistence classes, and any mapping metadata annotations found on them will be processed,
    or they will be mapped using the mapping annotation defaults defined by this specification.
    Such JAR files are specified relative to the directory or jar file that contains82 the root of the persistence unit.83

    8.2.1.6.4 List of Managed Classes
    A list of named managed persistence classes may be specified instead of, or in addition to, the JAR files
    and mapping files. Any mapping metadata annotations found on these classes will be processed, or they
    will be mapped using the mapping annotation defaults. The class element is used to list a managed
    persistence class.

    A list of all named managed persistence classes must be specified in Java SE environments to insure
    portability. Portable Java SE applications should not rely on the other mechanisms described here to
    specify the managed persistence classes of a persistence unit. Persistence providers may require that the
    set of entity classes and classes that are to be managed must be fully enumerated in each of the persistence.xml files in Java SE environments.

    Albert Lee.
  • infyRavi
    infyRavi
    14 Posts

    Re: Mentioning entity classes in Persistence.xml file

    ‏2011-07-13T13:50:24Z  
    • leealber
    • ‏2011-07-12T14:45:00Z
    Ravi,

    If you are deploying the application in JEE mode, you don't have to explicitly list the entity classes in the persistence unit. The container should search for the entities from the classpath. However JSE JPA application **MUST** enumerate the entities in the persistence unit using the <classes> element.

    One way to organize your application is to create a jar that packages all the entities and specify its location using the <jar-file> element in the persistence unit. See the examples in the 8.2.1.6.3 section below.

    Here are the related sections in the JPA spec.

    8.2.1.6.3 Jar Files
    One or more JAR files may be specified using the jar-file elements instead of, or in addition to the
    mapping files specified in the mapping-file elements. If specified, these JAR files will be searched
    for managed persistence classes, and any mapping metadata annotations found on them will be processed,
    or they will be mapped using the mapping annotation defaults defined by this specification.
    Such JAR files are specified relative to the directory or jar file that contains82 the root of the persistence unit.83

    8.2.1.6.4 List of Managed Classes
    A list of named managed persistence classes may be specified instead of, or in addition to, the JAR files
    and mapping files. Any mapping metadata annotations found on these classes will be processed, or they
    will be mapped using the mapping annotation defaults. The class element is used to list a managed
    persistence class.

    A list of all named managed persistence classes must be specified in Java SE environments to insure
    portability. Portable Java SE applications should not rely on the other mechanisms described here to
    specify the managed persistence classes of a persistence unit. Persistence providers may require that the
    set of entity classes and classes that are to be managed must be fully enumerated in each of the persistence.xml files in Java SE environments.

    Albert Lee.
    Hi,
    Yes we are using JEE deployment.
    should I put entity classes jar file under EAR/lib folder

    My persistence.xml file is under WAR --> WEB-INF-->META-INF folder.
    How should I define the path in <jar-file> in persistence.xml file.

    Thanks
    Ravi
  • leealber
    leealber
    19 Posts

    Re: Mentioning entity classes in Persistence.xml file

    ‏2011-07-13T15:41:28Z  
    • infyRavi
    • ‏2011-07-13T13:50:24Z
    Hi,
    Yes we are using JEE deployment.
    should I put entity classes jar file under EAR/lib folder

    My persistence.xml file is under WAR --> WEB-INF-->META-INF folder.
    How should I define the path in <jar-file> in persistence.xml file.

    Thanks
    Ravi
    Ravi,

    You can put the jar in many places as long as it can be reached from the classpath. EAR/lib is a natural location.

    For the <jar-file> usage, here are some example from the spec:

    "Such JAR files are specified relative to the directory or jar file that contains the root of the persistence unit."

    Example 1:
    app.ear
    lib/earEntities.jar
    earRootPUnit.jar (with META-INF/persistence.xml )
    persistence.xml contains:
    <jar-file>lib/earEntities.jar</jar-file>
    Example 2:
    app.ear
    lib/earEntities.jar
    lib/earLibPUnit.jar (with META-INF/persistence.xml )
    persistence.xml contains:
    <jar-file>earEntities.jar</jar-file>
    Example 3:
    app.ear
    lib/earEntities.jar
    ejbjar.jar (with META-INF/persistence.xml )
    persistence.xml contains:
    <jar-file>lib/earEntities.jar</jar-file>
    Example 4:
    app.ear
    war1.war
    WEB-INF/lib/warEntities.jar
    WEB-INF/lib/warPUnit.jar (with META-INF/persistence.xml )
    persistence.xml contains:
    <jar-file>warEntities.jar</jar-file>
    Example 5:
    app.ear
    war2.war
    WEB-INF/lib/warEntities.jar
    WEB-INF/classes/META-INF/persistence.xml
    persistence.xml contains:
    <jar-file>lib/warEntities.jar</jar-file>
    Example 6:
    app.ear
    lib/earEntities.jar
    war2.war
    WEB-INF/classes/META-INF/persistence.xml
    persistence.xml contains:
    <jar-file>../../lib/earEntities.jar</jar-file>
    Example 7:
    app.ear
    lib/earEntities.jar
    war1.war
    WEB-INF/lib/warPUnit.jar (with META-INF/persistence.xml )
    persistence.xml contains:
    <jar-file>../../../lib/earEntities.jar</jar-file>

    Albert.