JTA EntityManager instance in EJB 2.1 session bean - Websphere

This is a discussion on JTA EntityManager instance in EJB 2.1 session bean - Websphere ; Hi, Did anyone tried to obtain container managed EntityManager instance using JNDI lookup from a EJB 2.1 Session Bean on Websphere? I could do this on JBoss by adding the below property in persistence.xml file . However, I couldn't figure ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: JTA EntityManager instance in EJB 2.1 session bean

  1. JTA EntityManager instance in EJB 2.1 session bean

    Hi,



    Did anyone tried to obtain container managed EntityManager instance using JNDI lookup from a EJB 2.1 Session Bean on Websphere?

    I could do this on JBoss by adding the below property in persistence.xml file .

    However, I couldn't figure out a way to do the same on Websphere 6.1.0.15 with EJB3 feature pack. Can someone guide me in this regard?


    Best regards,

    Chandra.

  2. Re: JTA EntityManager instance in EJB 2.1 session bean

    Hi,
    WebSphere Application Server does not support EJB 3.0 or JPA functions from within EJB modules at the 2.1 or earlier level, so you would first need to ensure that your EJB 2.1 Session Bean is packaged in an EJB module with a deployment descriptor at the 3.0 level. You can then use the persistence-context-ref element within the ejb-jar.xml deployment descriptor to define a persistence context reference, and your EJB 2.1-style session bean can look up that reference within the session bean's java:comp/env namespace. Basically the persistence-context-ref XML DD stanza is analogous to the @PersistenceContext annotation (except the XML stanza doesn't perform injection unless you also add the injection-target directive to it). WebSphere Application Server does not presently provide a means to look up a persistence context in the global JNDI namespace, as that is not required by the JPA specification.

    The following text is from the Java EE v5 specification, and shows how to use the persistence-context-ref stanza inside an ejb-jar.xml descriptor (at the EJB 3.0 level):

    EE.5.13.1.3 Declaration of Persistence Context References in Deployment Descriptor

    Although a persistence context reference is an entry in the application component’s environment, the Bean Provider must not use an env-entry element to declare it.

    Instead, if metadata annotations are not used, the Bean Provider must declare all the persistence context references in the deployment descriptor using the persistence-context-ref elements. This allows the Application Assembler or Deployer to discover all the persistence context references used by an application component. Deployment descriptor entries may also be used to specify injection of a persistence context reference into a bean.

    Each persistence-context-ref element describes a single container-managed entity manager reference. The persistence-context-ref element consists of the optional description, persistence-unit-name, persistence-context-type, and persistence-property elements, and the mandatory persistence-context-ref-name element.

    The persistence-context-ref-name element contains the name of the environment entry used in the application component’s code. The name of the environment entry is relative to the java:comp/env context (e.g., the name should be persistence/InventoryAppMgr rather than java:comp/env/persistence/InventoryAppMgr). The persistence-unit-name element is the name of the persistence unit, as specified in the persistence.xml file for the persistence unit, and must be specified if multiple persistence units exist in the scope of the referencing component. The persistence-context-type element specifies whether a transaction-scoped or extended persistence context is to be used. Its value is either Transaction or Extended. If the persistence context type is not specified, a transaction-scoped persistence context will be used. The optional persistence-property elements specify configuration properties that are passed to the persistence provider when the entity manager is created.

    The following example is the declaration of a persistence context reference used by the InventoryManager enterprise bean illustrated in the previous subsection (of the EE v5 spec).
    ....

    <persistence-context-ref>
    <description>
    Persistence context for the inventory management application.
    </description>
    <persistence-context-ref-name>
    persistence/InventoryAppDB
    </persistence-context-ref-name>
    <persistence-unit-name>
    InventoryManagement
    </persistence-unit-name>
    </persistence-context-ref>

    ....


    So in the above example, your EJB 2.1 session bean (packaged in an EJB 3.0 module) would look up the persistence context using the name "java:comp/env/persistence/InventoryAppDB" .

    Hope this helps.
    -- Randy


    chandrasekhares@gmail.com wrote:
    > Hi,
    >
    >
    >
    > Did anyone tried to obtain container managed EntityManager instance using JNDI lookup from a EJB 2.1 Session Bean on Websphere?
    >
    > I could do this on JBoss by adding the below property in persistence.xml file .
    >
    > However, I couldn't figure out a way to do the same on Websphere 6.1.0.15 with EJB3 feature pack. Can someone guide me in this regard?
    >
    >
    > Best regards,
    >
    > Chandra.


  3. Re: JTA EntityManager instance in EJB 2.1 session bean

    Hi Randy,

    Thanks for your reply!
    I actually got it working on Websphere using OpenJPA as well as IBM
    WebSphere JPA (default on websphere) as JPA providers.
    However, my application requires to use Hibernate as the JPA provider.
    To enable this, I did the following:
    a) changed provider to
    org.hibernate.ejb.HibernatePersistence in
    persistence.xml.
    b) set "Class loaded with application class loader first" in
    Application servers > server1 > Class loader ->
    Classloader_1215581369163 on WAS admin console. Also added a shared
    library reference, that holds all the required hibernate core and
    Entity Manager related jar files.
    c) Created a custom property to set Hibernate as my default JPA
    provider on WAS admin console, Application servers > server1 > Process
    Definition > Java Virtual Machine > Custom Properties. Added this
    property "com.ibm.websphere.jpa.default.provider " with the value
    "org.hibernate.ejb.HibernatePersistence".

    Now, when I deploy the application from RAD following JPA related
    messages are displayed on the console:
    [7/9/08 12:19:09:620 IST] 0000000a JPAComponentI I CWWJP0026I: The
    Java Persistence API (JPA) component is initializing.
    [7/9/08 12:19:09:666 IST] 0000000a JPAComponentI I CWWJP0006I: The
    org.hibernate.ejb.HibernatePersistence class is loaded as the default
    Java Persistence API (JPA) provider.
    [7/9/08 12:19:09:713 IST] 0000000a JPAComponentI I CWWJP0027I: The
    Java Persistence API (JPA) component has initialized.
    [7/9/08 12:19:21:803 IST] 0000000a JPAComponentI I CWWJP0028I: The
    Java Persistence API (JPA) component is starting.
    [7/9/08 12:19:21:803 IST] 0000000a JPAComponentI I CWWJP0017I: The
    Java Persistence API (JPA) component has started.
    [7/9/08 12:19:29:784 IST] 0000002b JPAPUnitInfo E CWWJP0009E: The
    server cannot create an EntityManangerFactory factory for the BLWJPA
    persistent unit from the org.hibernate.ejb.HibernatePersistence
    provider in file:/D:/Repository_blw_core_2008/trunk/Source/BM-
    Resale_V16_DevVersion/Application/PersistenceJPA/build/ module.

    The last message clearly shows that the EntityMangerFactory instance
    is not created. This happens only when I try to use Hibernate as the
    JPA provider (may be this is the case with all third party JPA
    providers as well), not in the case of OpenJPA or Websphere JPA which
    are default.

    Best regards,
    Chandra.

+ Reply to Thread