1 Reply Latest reply on Apr 29, 2008 4:30 AM by jaikiran pai

    Setting persistence JNDI name for PersistenceContext

    Dean Pullen Novice

      We have a scenario where we have a common EJB (ejbcommon.jar) and another EJB (ejbproject.jar) which references this common EJB.

      You can pretty much see the setup in this other thread:
      http://www.jboss.com/index.html?module=bb&op=viewtopic&t=131583

      i.e.
      ejbproject.jar contains the persistence.xml, which the ejbs in ejbcommon.jar reference by using:
      @PersistenceContext(unitName = "XXXejbproject.jar#our-par")

      The trouble comes when we switch projects within our build and use ejbproject2.jar. The common jar is now broken.

      We have several possibilities including filtering that token during the build, but this kinda breaks the maven philosphy - the ejbcommon.jar within the maven repository will be over-written every time we switch project builds.

      My solution would be to force the JNDI name of the peristence unit for each of the ejbproject.jar and ejbproject2.jar to something common like 'ejb.jar' and then reference this within the ejbcommon.jar.

      Thus, how do you force the ejb jar that contains the peristence.xml to have a specific JNDI name?

        • 1. Re: Setting persistence JNDI name for PersistenceContext
          jaikiran pai Master

          The @PersistenceContext does not accept an attribute like jndiName, where you could have specified the jndi-name of the entitymanager.

          However, i do see a way out. There's a @Resource annotation which accepts a mappedName attribute (which is nothing but the jndi-name of a resource) and also the "type" of the resource. So here's what you can do:

          1) Bind the entitymanager to the JNDI. To do this, you have to add the following 2 properties to your persistence.xml:

          <?xml version="1.0" encoding="UTF-8"?>
          
          <persistence xmlns="http://java.sun.com/xml/ns/persistence">
          
           <persistence-unit name="msp-par">
           <jta-data-source>blahblahblah</jta-data-source>
           .....
           .....
           <properties>
           <property name="jboss.entity.manager.jndi.name" value="java:/MyEntityManager"/>
           <property name="jboss.entity.manager.factory.jndi.name" value="java:/MyEntityManagerFactory"/>
           </properties>
           </persistence-unit>
          </persistence>


          2) In your bean, inject the persistence context as follows:
          @Resource (type = EntityManager.class, mappedName = "java:/MyEntityManager")
          private EntityManager entityManager;


          Does this solve your problem?