3 Replies Latest reply on Jan 3, 2003 5:14 PM by Collin

    Create an entity bean with CMP

    baudouin Newbie


      I would like to retrieve an entity bean with a finder method.

      i tried my own method (findLast) and findByPrimaryKey but
      in the two cases Jboss seems to meet problems for
      creating the bean (java.lang.NullPointerException in org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager.createBeanClassInstance).

      All my beans are deployed (see with jmx-console) and i
      don't understant why it can't create one bean.

      Thanks.

        • 1. Re: Create an entity bean with CMP
          Collin Novice

          Can you post some of your bean definition code?

          • 2. Re: Create an entity bean with CMP
            baudouin Newbie


            yes, surely.
            i developed beans with xdoclet for generating interfaces
            and DD.


            * @xdoclet-generated at 31 déc. 02 15:20:00
            */
            public interface SaisonLocalHome extends javax.ejb.EJBLocalHome
            {
            public static final String COMP_NAME="java:comp/env/ejb/Saison";
            public static final String JNDI_NAME="orvault/entity/SaisonLocal";

            /**
            * Create a new saison
            */
            public orvault.interfaces.SaisonLocal create(java.lang.Integer newid,java.lang.String newlibelle) throws javax.ejb.CreateException;

            public orvault.interfaces.SaisonLocal findByPrimaryKey(java.lang.Integer pk) throws javax.ejb.FinderException;
            public orvault.interfaces.SaisonLocal findCurrent() throws javax.ejb.FinderException;
            public orvault.interfaces.SaisonLocal findLast() throws javax.ejb.FinderException;
            public java.util.Collection findAll() throws javax.ejb.FinderException;

            }


            /**
            * Local interface for Saison.
            * @author XDOCLET 1.1.2
            * @xdoclet-generated at 31 déc. 02 15:20:00
            */
            public interface SaisonLocal extends javax.ejb.EJBLocalObject
            {
            /**
            * Saison Cours
            */
            public java.lang.Integer getCours( ) ;

            /**
            * Saison id
            */
            public java.lang.Integer getId( ) ;

            /**
            * Saison libelle
            */
            public java.lang.String getLibelle( ) ;

            /**
            * Saison Cours
            */
            public void setCours( java.lang.Integer cours ) ;

            /**
            * Saison libelle
            */
            public void setLibelle( java.lang.String lib ) ;

            }


            implementation bean.

            /**
            * This is a saison bean.
            * @author Thierry BAUDOUIN
            *
            * @ejb:bean name="Saison"
            type="CMP" cmp-version="2.x" view-type="local"
            jndi-name="orvault/entity/Saison" local-jndi-name="orvault/entity/SaisonLocal"
            primkey-field="id"
            * @ejb:finder signature="orvault.interfaces.SaisonLocal findCurrent()"
            unchecked="true" transaction-type="Supports"
            query="SELECT DISTINCT OBJECT(D) FROM Saison AS d WHERE d.B_COURS = 1"
            * @ejb:finder signature="orvault.interfaces.SaisonLocal findLast()"
            unchecked="true" transaction-type="Supports"
            query="SELECT DISTINCT OBJECT(D) FROM Saison AS d WHERE d.N_CODESAIS in ( SELECT MAX(N_CODESAIS) from Saison )"
            * @ejb:finder signature="java.util.Collection findAll()" unchecked="true" transaction-type="NotSupported"
            * @ejb:home remote-class="orvault.interfaces.SaisonHome" local-class="orvault.interfaces.SaisonLocalHome"
            * @ejb:interface remote-class="orvault.interfaces.Saison" local-class="orvault.interfaces.SaisonLocal"
            * @ejb:pk class="java.lang.Integer" generate="false" unchecked="true"
            * @ejb:data-object package="orvault.interfaces"
            *
            * @jboss:table-name "saison"
            * @jboss:tuned-updates "true"
            */
            public abstract class SaisonBean implements javax.ejb.EntityBean{

            // Public -------------------------------------------------------------------

            /**
            * Saison id
            * @ejb:pk-field
            * @ejb:interface-method
            * @ejb:persistent-field
            * @jboss:column-name "N_CODESAIS"
            */
            public abstract Integer getId();

            /** Saison id (not remote) */
            public abstract void setId(Integer id);

            /**
            * Saison libelle
            * @ejb:interface-method
            * @ejb:persistent-field
            * @jboss:column-name "C_LIBELLESAIS"
            */
            public abstract String getLibelle();

            /**
            * Saison libelle
            * @ejb:interface-method
            */
            public abstract void setLibelle(String lib);

            /**
            * Saison Cours
            * @ejb:interface-method
            * @ejb:persistent-field
            * @jboss:column-name "B_COURS"
            */
            public abstract Integer getCours();

            /**
            * Saison Cours
            * @ejb:interface-method
            */
            public abstract void setCours(Integer cours);



            // Implementation of EntityBean ---------------------------------------------

            /**
            * Create a new saison
            * @ejb:create-method
            */
            public Integer ejbCreate(Integer newid, String newlibelle) throws javax.ejb.CreateException{
            setLibelle(newlibelle);
            setId(newid);
            setCours(new Integer(0));
            return newid;
            }

            /** Create a new saison */
            public void ejbPostCreate(Integer newid, String newlibelle) throws javax.ejb.CreateException{

            }

            /**
            * Remove this saison
            * @ejb:transaction type="Mandatory"
            */
            public void ejbRemove() throws javax.ejb.RemoveException, javax.ejb.EJBException {}
            }


            I use this entity bean inside a session bean like this :

            public class AdminManager implements javax.ejb.SessionBean{

            // Attributes ---------------------------------------------------------------

            //Runtime contexts...
            private SessionContext sctx = null;
            private transient InitialContext ctx = null;

            //EJB references...
            private SaisonLocalHome saisonhome = null;

            //Environment entries...
            private String datasource = null;


            // Public -------------------------------------------------------------------

            /**
            * Create a new Saison
            * @ejb:interface-method view-type="both"
            * @ejb:transaction type="Required"
            */
            public void createSaison(String newsaison ) throws EJBException{
            try{
            //Retrieve homes if null.
            System.out.println ("Debut create saison");
            if (saisonhome == null) saisonhome = (SaisonLocalHome)ctx.lookup("java:comp/env/ejb/Saison");
            System.out.println ("saison home " + saisonhome.toString());
            //Create the new saison
            SaisonLocal maxsaison = saisonhome.findByPrimaryKey(new Integer(1));
            System.out.println ("saison home max " + maxsaison.toString());
            SaisonLocal newsaisoncreated = saisonhome.create(new Integer(maxsaison.getId().intValue() + 1), newsaison);
            }
            catch (Exception e) {throw new EJBException(e);}
            }


            i have the error (see first message) when it execute saisonhome.findByPrimaryKey(new Integer(1));. my variable "saisonhome" is good value.

            thanks for your help.


            • 3. Re: Create an entity bean with CMP
              Collin Novice

              Well, one thing I have noticed in my beans is that when I write my EJB-QL, I have to write my field references with the bean-derived name of the field.

              In other words, if I have a getName and setName pair that relate to CMP fields, then in my EJB-QL, I would write something like

              query="SELECT OBJECT(c) from Data AS c WHERE c.name = ?1"

              Even if I had specified that my column persistence name was something like "objectName". That caused me grief for about two days.

              Also, I'm not sure if EJB-QL is case sensitive, but if so, you've got some case mismatches in there.

              I hope that helps,
              Collin