1 Reply Latest reply on Oct 14, 2004 1:08 PM by tjclifford

    JBoss says bean is not a jbosscmp-jdbc-managed cmp entity

    tjclifford

      Hello:

      As part of my own self-education in EJB, I am trying to get
      some of Brett McLaughlin's code from
      Java Enterprise Applications, Vol I (O'Reilly, 2002)
      to work in JBoss.

      I have implemented only the Office part of his mySql database,
      that part of his bean code, and have tried to implement his
      stateless session bean 'SequenceBean' to look up the next key
      available to add offices.

      I have set up the jbosscmp-jdbc.xml and ejb-jar.xml, I believe,
      according to examples I have seen, but I still get the following error
      when I deploy the jar file:

      -----------------------------------------

      22:38:56,971 INFO [EjbModule] Deploying Office
      22:38:56,981 INFO [EjbModule] Deploying SequenceBean
      22:38:57,351 ERROR [EntityContainer] Starting failed jboss.j2ee:jndiName=Office,
      service=EJB
      org.jboss.deployment.DeploymentException: Configuration found in jbosscmp-jdbc.x
      ml for entity SequenceBean but bean is not a jbosscmp-jdbc-managed cmp entity in
      ejb-jar.xml
      at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCApplicationMetaData.(JDBCApplicationMetaData.java:358)
      at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCXmlFileLoader.load(JDBCXm
      lFileLoader.java:75)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.loadJDBCEntityMetaDat
      a(JDBCStoreManager.java:726)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.initStoreManager(JDBC
      StoreManager.java:419)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.start(JDBCStoreManage
      r.java:363)
      at org.jboss.ejb.plugins.CMPPersistenceManager.start(CMPPersistenceManag
      er.java:157)
      at org.jboss.ejb.EntityContainer.startService(EntityContainer.java:340)
      at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanS
      upport.java:271)
      ......et. cetera......

      -----------------------------------------

      when I try to leave out the jbosscmp-ejb.xml file completely,
      the jar file deploys without errors, but when I run the client,
      it gives me an error that jdbc is not bound:

      ------------------------------------------
      >java com.sample.client.EntityCreator

      Clifford IT Services EntityCreator program.

      setting security policy...
      got InitialContext...
      ...is: [javax.naming.InitialContext@291aff]
      Looking up the Office bean.
      got context...
      refobj = OfficeHome
      create DallasOffice...
      Error in EntityCreator: OfficeBean: Error getting primary key value: Sequence Be
      an Exception: Error getting JDBC resources: jdbc not bound: jdbc not bound
      c:\Dl\Coding\Java\Ejb\Jboss\Db1
      >


      -------------------------------------------

      I can email a zip file with the pertinent files included,
      if anyone might be interested in this issue. Maybe it's just
      my limited educational resources, but this has me puzzled.

      I've checked the JBoss forums and do not see anything
      that helps much. A google search does not come up
      with much, either.

      Thanks for anything that you might point out.

      I'm running JBoss v4.0.0RC2, and have tried the same with
      RC1, and with JBoss v2.4.2.
      I'm running Apache 2.0.48 and Tomcat V5 on the same machine.

      Tom C.

        • 1. Re: JBoss says bean is not a jbosscmp-jdbc-managed cmp entit
          tjclifford

          As a follow-up, I thought I should post at least some part of the
          files I'm working with.

          The OfficeBean:

          package com.sample.ejb.office;

          import javax.ejb.CreateException;
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
          import javax.rmi.*;
          import java.rmi.*;

          import com.sample.ejb.sequence.SequenceException;
          import com.sample.ejb.sequence.SequenceLocal;
          import com.sample.ejb.sequence.SequenceLocalHome;
          //import com.sample.ejb.sequence.SequenceHome;
          import com.sample.ejb.util.EntityAdapter;

          public abstract class OfficeBean extends EntityAdapter {

          public Integer ejbCreate(String city, String state)
          throws CreateException {

          // Get the next primary key value
          try {


          System.out.println( "OfficeBean: get InitialContext..." );
          Context ctx = new InitialContext( );
          System.out.println( "OfficeBean: got InitialContext..." );
          System.out.println( "OfficeBean: ...is: [" + ctx + "]" );

          // Note that RMI-IIOP narrowing is not required
          System.out.println("OfficeBean: looking up the SequenceLocalHome...");
          SequenceLocalHome home = (SequenceLocalHome)
          ctx.lookup("local/SequenceBean");
          System.out.println("OfficeBean: SequenceLocal.create()...");
          SequenceLocal sequence = home.create();
          System.out.println("OfficeBean: looking up OfficeKey...");
          String officeKey =
          (String)ctx.lookup("java:comp/env/constants/OfficeKey");
          Integer id = sequence.getNextValue(officeKey);

          // Set values
          setId(id);
          setCity(city);
          setState(state);

          return null;
          } catch (NamingException e) {
          throw new CreateException("OfficeBean: Could not obtain an " +
          "InitialContext.\n msg: [" + e.getMessage() + "]\n");
          } catch (SequenceException e) {
          throw new CreateException("OfficeBean: Error getting primary key value: " +
          e.getMessage());
          }
          }

          public void ejbPostCreate(String city, String state) {
          // Empty implementation
          }

          public OfficeInfo getInfo() {
          return new OfficeInfo(getId().intValue(), getCity(), getState());
          }

          public void setInfo(OfficeInfo officeInfo) {
          setId(new Integer(officeInfo.getId()));
          setCity(officeInfo.getCity());
          setState(officeInfo.getState());
          }

          public abstract Integer getId();
          public abstract void setId(Integer id);

          public abstract String getCity();
          public abstract void setCity(String city);

          public abstract String getState();
          public abstract void setState(String state);
          }


          The SequenceBean:

          package com.sample.ejb.sequence;

          import java.sql.Connection;
          import java.sql.DriverManager;
          import java.sql.ResultSet;
          import java.sql.SQLException;
          import java.sql.PreparedStatement;
          import java.sql.DatabaseMetaData;
          import javax.ejb.CreateException;
          import javax.ejb.EJBException;
          import javax.ejb.SessionBean;
          import javax.naming.Context;
          import javax.naming.InitialContext;
          import javax.naming.NamingException;
          import javax.sql.DataSource;

          import com.sample.ejb.util.SessionAdapter;

          public class SequenceBean extends SessionAdapter {

          /** The query to get the next value from the keys table */
          private static final String selectQuery =
          new StringBuffer("select next_value ")
          .append("from primary_keys ")
          .append("where key_name = ?")
          .toString();

          /** The query to update the next value in the keys table */
          private static final String updateQuery =
          new StringBuffer("UPDATE primary_keys ")
          .append(" SET NEXT_VALUE = ? ")
          .append(" WHERE KEY_NAME = ?")
          .toString();

          public void ejbCreate() throws CreateException {
          // No action required for stateless session beans
          }

          public Integer getNextValue(String keyName) throws SequenceException {
          int returnValue = 0;

          Connection con = null;
          PreparedStatement pstmt = null;
          ResultSet rs = null;
          DatabaseMetaData dmd = null;
          ResultSet table_list = null;
          ResultSet cat_list = null;
          String[] types = {"TABLE"};

          try {
          //Context context = new InitialContext(System.getProperties());
          System.out.println( "SequenceBean: get InitialContext..." );
          Context context = new InitialContext();
          System.out.println("SequenceBean: got context: " + context);
          System.out.println("SequenceBean: looking up the DataSource...");
          DataSource ds =
          (DataSource)
          context.lookup("java:comp/env/jdbc/sampledb");
          System.out.println("SequenceBean: datasource: " + ds);
          //System.out.println("SequenceBean: datasource name: " + ds.getName());
          con = ds.getConnection();
          System.out.println("SequenceBean: got connection: " + con);
          dmd = con.getMetaData();
          System.out.println("SequenceBean: connection product: " +
          dmd.getDatabaseProductName());

          try {
          table_list = dmd.getTables(null, null, "%", types);

          // Get the table names
          while (table_list.next()) {
          // Get the table name
          String tableName = table_list.getString(3);

          // Get the table's catalog and schema names (if any)
          String tableCatalog = table_list.getString(1);
          String tableSchema = table_list.getString(2);
          System.out.println("SequenceBean: table in db: " + tableName);
          } // end try...
          } catch (SQLException e) {
          }


          pstmt = con.prepareStatement(selectQuery);
          pstmt.setString(1, keyName);
          System.out.println("SequenceBean: executing query: " + pstmt);
          rs = pstmt.executeQuery();

          if (rs.next()) {
          returnValue = rs.getInt("NEXT_VALUE");

          pstmt = con.prepareStatement(updateQuery);
          pstmt.setInt(1, returnValue + 1);
          pstmt.setString(2, keyName);
          System.out.println("SequenceBean: got keyName: " + keyName);
          pstmt.executeUpdate();
          } else {
          // Close connections before throwing the exception
          try {
          rs.close();
          } catch (Exception ignored) { }
          try {
          pstmt.close();
          } catch (Exception ignored) { }
          try {
          con.close();
          } catch (Exception e) { }
          System.out.println("SequenceBean: Could not obtain a key " +
          "value for the key name " + keyName);
          //throw new SequenceException("Could not obtain a key " +
          // "value for the key name " + keyName);
          }
          } catch (NamingException e) {
          throw new SequenceException("Error getting JDBC " +
          "resources: " + e.getMessage(), e);
          //System.out.println("SequenceBean: Error getting JDBC " +
          // "resources: " + e.getMessage(), e);
          } catch (SQLException e) {
          throw new SequenceException("Error in SQL: " +
          e.getMessage(), e);
          //System.out.println("SequenceBean: Error in SQL: " +
          // e.getMessage(), e);
          } finally {
          try {
          rs.close();
          } catch (Exception ignored) { }
          try {
          pstmt.close();
          } catch (Exception ignored) { }
          try {
          con.close();
          } catch (Exception ignored) { }
          }

          return new Integer(returnValue);
          }
          }

          The jbosscmp-jdbc.xml:

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE jbosscmp-jdbc PUBLIC
          "-//JBoss//DTD JBOSSCMP-JDBC 3.2//EN"
          "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_3_2.dtd">

          <jbosscmp-jdbc>


          java:/jdbc/mySQLDS
          <datasource-mapping>mySQL</datasource-mapping>
          <create-table>false</create-table>
          <remove-table>false</remove-table>
          <read-only>false</read-only>
          <read-time-out>300000</read-time-out>
          <row-locking>false</row-locking>
          <pk-constraint>true</pk-constraint>
          <fk-constraint>false</fk-constraint>
          <preferred-relation-mapping>foreign-key</preferred-relation-mapping>
          <read-ahead>
          on-load
          <page-size>500</page-size>
          <eager-load-group>group name</eager-load-group>
          </read-ahead>


          <enterprise-beans>

          <!--
          <ejb-name>OfficeBean</ejb-name>
          <table-name>offices</table-name>
          <cmp-field>
          <field-name>office_id</field-name>
          <column-name>OFFICE_ID</column-name>
          </cmp-field>
          <cmp-field>
          <field-name>city</field-name>
          <column-name>CITY</column-name>
          </cmp-field>
          <cmp-field>
          <field-name>state</field-name>
          <column-name>STATE</column-name>
          </cmp-field>
          -->


          <ejb-name>SequenceBean</ejb-name>
          <table-name>primary_keys</table-name>
          <cmp-field>
          <field-name>key_name</field-name>
          <column-name>KEY_NAME</column-name>
          </cmp-field>
          <cmp-field>
          <field-name>next_value</field-name>
          <column-name>NEXT_VALUE</column-name>
          </cmp-field>


          </enterprise-beans>

          </jbosscmp-jdbc>


          The ejb-jar.xml:

          <?xml version="1.0"?>

          <!DOCTYPE ejb-jar PUBLIC
          "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
          "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

          <ejb-jar>

          <enterprise-beans>



          This Office bean represents a sample office,
          including its location.

          <display-name>OfficeBean</display-name>
          <ejb-name>Office</ejb-name>
          com.sample.ejb.office.OfficeHome
          com.sample.ejb.office.Office
          <local-home>com.sample.ejb.office.OfficeLocalHome</local-home>
          com.sample.ejb.office.OfficeLocal
          <ejb-class>com.sample.ejb.office.OfficeBean</ejb-class>
          <persistence-type>Container</persistence-type>
          False
          <cmp-version>2.x</cmp-version>

          <abstract-schema-name>OFFICES</abstract-schema-name>
          <cmp-field>
          <field-name>id</field-name>
          </cmp-field>
          <cmp-field>
          <field-name>city</field-name>
          </cmp-field>
          <cmp-field>
          <field-name>state</field-name>
          </cmp-field>
          <primkey-field>id</primkey-field>
          <prim-key-class>java.lang.Integer</prim-key-class>

          <resource-ref>
          Connection to the sample database.
          <res-ref-name>jdbc/sampledb</res-ref-name>
          <resource-name>jdbc/sampledb</resource-name>
          <init-param driver-name="com.mysql.jdbc.Driver"/>
          <init-param user="tom"/>
          <init-param password="password"/>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
          </resource-ref>

          <env-entry>
          <env-entry-name>constants/OfficeKey</env-entry-name>
          <env-entry-type>java.lang.String</env-entry-type>
          <env-entry-value>OFFICES</env-entry-value>
          </env-entry>





          This Sequence bean allows entity beans to obtain primary key
          values as if from a sequence.

          <ejb-name>SequenceBean</ejb-name>
          <local-home>com.sample.ejb.sequence.SequenceLocalHome</local-home>
          com.sample.ejb.sequence.SequenceLocal
          <ejb-class>com.sample.ejb.sequence.SequenceBean</ejb-class>
          <session-type>Stateless</session-type>
          <!-- 'Container' or 'Bean' ?? -->
          <transaction-type>Container</transaction-type>

          <ejb-local-ref>
          <ejb-ref-name>SequenceLocalHome</ejb-ref-name>
          <ejb-ref-type>Entity</ejb-ref-type>
          <local-home>com.sample.ejb.sequence.SequenceLocalHome</local-home>
          com.sample.ejb.sequence.SequenceLocal
          <ejb-link>SequenceBean</ejb-link>
          </ejb-local-ref>

          <resource-ref>
          Connection to the sample database.
          <res-ref-name>jdbc/sampledb</res-ref-name>
          <resource-name>jdbc/sampledb</resource-name>
          <init-param driver-name="com.mysql.jdbc.Driver"/>
          <init-param user="tom"/>
          <init-param password="password"/>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
          </resource-ref>




          </enterprise-beans>

          <assembly-descriptor>

          <security-role>
          the ones who have full access to the EJB
          <role-name>everyone</role-name>
          </security-role>

          <method-permission>
          <role-name>everyone</role-name>
          <ejb-name>SequenceBean</ejb-name>
          <method-name>*</method-name>
          </method-permission>

          <container-transaction>

          <ejb-name>SequenceBean</ejb-name>
          <method-name>*</method-name>

          <trans-attribute>Required</trans-attribute>
          </container-transaction>

          </assembly-descriptor>



          </ejb-jar>


          Thanks.