Accessing an entity bean from a session bean using local intf

Discussions

EJB programming & troubleshooting: Accessing an entity bean from a session bean using local intf

  1. Hi,
      Could anyone provide me with some sample ejb-jar.xml and weblogic-ejb-jar.xml file for achieving this. Actually I've deployed an entity bean A successfully and while trying to access this from the session bean B, I've given the ejb-ref parameters in the ejb-jar.xml file of B. but while deploying weblogic gives error saying the xml files are not proper.

    Thanks
    Ansuman
  2. I am doing this successfully with about 30 entity beans using WebLogic server 6.1. If you still need some examples, post your email here and I'll email you two example files.
    Judd Smith
  3. I still have the problem Judd. Actually, I'm able to deploy the session bean and the entity bean only if they are in the same jar file. However if I've deployed the entity bean earlier and then trying to deploy the session bean, I get an error message saying there is no such entity bean. I'd really appreciate if you could post the xml files while deploying the session bean in a diff jar file and while deploying the entity bean in a diff jar file. I'm using WebLogic 6.1 SP1.

    Thanks
    Ansu
  4. Hi,

    You might have used ejb-ref instead of ejb-local-ref in ejb-jar.xml. Use ejb-local-ref and it should work.

    Actually, when you want to access an EJB via its local interface then your calling EJB MUST be packaged in the same JAR. EJBs with ONLY local interfaces are accessible only from the JAR the are pachaged in.

    Thats why the secondly deployed session bean had not seen the entity with local interface.

    HTH,

    Tibor
  5. I've copied the two xml files for one of my entity beans below. I do remember that in order to get it to work I did have to place both jar files (i.e., the session bean jar file and entity bean jar file) in the same EAR file and deploy the EAR. Local interfaces only work if they are inside the same EAR.
    Good luck,
    Judd

    Here is the ejb-jar.xml file ...
    <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>


    <!-- Generated XML! -->

    <ejb-jar>
      <enterprise-beans>
        <entity>
          <ejb-name>Auditelements</ejb-name>
          <home>com.fujitsu.ftxs.corema.ccs.rei.ebeans.AuditelementsHome</home>
          <remote>com.fujitsu.ftxs.corema.ccs.rei.ebeans.Auditelements</remote>
          <local-home>com.fujitsu.ftxs.corema.ccs.rei.ebeans.AuditelementsLocalHome</local-home>
          <local>com.fujitsu.ftxs.corema.ccs.rei.ebeans.AuditelementsLocal</local>
          <ejb-class>com.fujitsu.ftxs.corema.ccs.rei.ebeans.AuditelementsBean</ejb-class>
          <persistence-type>Container</persistence-type>
          <prim-key-class>com.fujitsu.ftxs.corema.ccs.rei.ebeans.AuditelementsPK</prim-key-class>
          <reentrant>False</reentrant>
          <abstract-schema-name>Auditelements</abstract-schema-name>
          <cmp-field>
            <field-name>auditid</field-name>
          </cmp-field>
          <cmp-field>
            <field-name>sequencenbr</field-name>
          </cmp-field>
          <cmp-field>
            <field-name>customerid</field-name>
          </cmp-field>
          <cmp-field>
            <field-name>datetime</field-name>
          </cmp-field>
          <cmp-field>
            <field-name>userid</field-name>
          </cmp-field>
          <cmp-field>
            <field-name>reasonid</field-name>
          </cmp-field>
          <cmp-field>
            <field-name>comment</field-name>
          </cmp-field>
          <cmp-field>
            <field-name>datatypeid</field-name>
          </cmp-field>
          <cmp-field>
            <field-name>entityid</field-name>
          </cmp-field>
          <cmp-field>
            <field-name>elementid</field-name>
          </cmp-field>
          <cmp-field>
            <field-name>beforeimage</field-name>
          </cmp-field>
          <cmp-field>
            <field-name>afterimage</field-name>
          </cmp-field>
          <resource-ref>
            <res-ref-name>jdbc/DataSource</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
          </resource-ref>
          <query>
            <query-method>
              <method-name>findAll</method-name>
              <method-params>
              </method-params>

            </query-method>

            <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM Auditelements o ]]></ejb-ql>
          </query>
          <query>
            <query-method>
              <method-name>findByCustomerID</method-name>
              <method-params>
                <method-param>int</method-param>
              </method-params>

            </query-method>

            <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM Auditelements o WHERE (customerid = ?1)]]></ejb-ql>
          </query>
          <query>
            <query-method>
              <method-name>findByAuditID</method-name>
              <method-params>
                <method-param>int</method-param>
              </method-params>

            </query-method>

            <ejb-ql><![CDATA[SELECT DISTINCT OBJECT(o) FROM Auditelements o WHERE (auditid = ?1)]]></ejb-ql>
          </query>
        </entity>
      </enterprise-beans>

      <assembly-descriptor>
        <container-transaction>
          <method>
            <ejb-name>Auditelements</ejb-name>
            <method-name>*</method-name>
          </method>
          <trans-attribute>Required</trans-attribute>
        </container-transaction>
      </assembly-descriptor>

    </ejb-jar>

    Here is the weblogic-ejb-jar.xml file ...
    <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB//EN' 'http://www.bea.com/servers/wls600/dtd/weblogic-ejb-jar.dtd'>


    <!-- Generated XML! -->

    <weblogic-ejb-jar>
      <weblogic-enterprise-bean>
        <ejb-name>Auditelements</ejb-name>
        <entity-descriptor>
          <pool>
          </pool>

          <entity-cache>
          </entity-cache>

          <lifecycle>
          </lifecycle>

          <persistence>
            <persistence-type>
              <type-identifier>WebLogic_CMP_RDBMS</type-identifier>
              <type-version>6.0</type-version>
              <type-storage>META-INF/weblogic-cmp-rdbms-jar.xml</type-storage>
            </persistence-type>
            <persistence-use>
              <type-identifier>WebLogic_CMP_RDBMS</type-identifier>
              <type-version>6.0</type-version>
            </persistence-use>

          </persistence>

          <entity-clustering>
          </entity-clustering>

        </entity-descriptor>

        <transaction-descriptor>
        </transaction-descriptor>

        <reference-descriptor>
          <resource-description>
            <res-ref-name>jdbc/DataSource</res-ref-name>
            <jndi-name>DataSource</jndi-name>
          </resource-description>
        </reference-descriptor>

        <jndi-name>Auditelements</jndi-name>
        <local-jndi-name>AuditelementsLocal</local-jndi-name>
      </weblogic-enterprise-bean>
    </weblogic-ejb-jar>
  6. I was using ejb-local-ref. The only way I could get around was by having all the beans in the same jar file. However I don't see this kind of restriction on the ejb specs. I'd assume this to be a problem with the weblogic server. I mean if I already have a bean deployed, why should I package it again, if I want to access it.

    Anyway, thanks everyone for your answers. Guess I'm assuming that weblogic 6.1 sp1 does not support accessing other beans using ejb-ref or ejb-local-ref, if they are not in the same jar or ear file... Is that a safe conclusion?

    Thanks
    Ansu
  7. They don't need to be in the same jar file but they do need to be in the same ear file.
    Judd
  8. I have not worked with EJB 2.0 but i read somewhere of the class-path variable in the manifest file.Basically u set this variable name to the Entitybean Name u want to refer too.
    (Check it out.I am sure of this)