3 Replies Latest reply on Jan 2, 2002 3:57 PM by Dain Sundstrom

    How to resolve the "id may not be null"

    IPOz Newbie

      Hi,friends
      I am not familiar with EntityBean.Recently i changed a session bean to entity bean as below:
      home interface:
      ...
      public interface CoffeesHome extends javax.ejb.EJBHome
      {
      public Coffees create() throws RemoteException, CreateException;
      }
      remote interface
      ...
      public interface Coffees extends EJBObject {
      public RowSet getCoffees() throws RemoteException, SQLException;
      public void updateCoffees(RowSet rs) throws RemoteException, SQLException;
      }
      entity bean
      ...
      public class CoffeesEJB implements EntityBean
      {
      private EntityContext ctx = null;
      // EJB create method
      public Object ejbCreate() throws CreateException
      {
      return null;
      }
      public void ejbPostCreate() throws CreateException
      {
      }
      //business method
      private Connection getConnection() throws Exception
      {
      }

      public RowSet getCoffees() throws SQLException
      {
      }

      public void updateCoffees(RowSet rs) throws SQLException
      {
      }

      public void setEntityContext(EntityContext ec)
      {
      this.ctx = ec;
      }
      public void unsetEntityContext()
      {
      this.ctx = null;
      }
      public void ejbRemove() {}
      public void ejbPassivate() {}
      public void ejbActivate() {}
      public void ejbLoad() {}
      public void ejbStore() {}
      }
      ejb-jar.xml

      <ejb-name>CoffeesEJB</ejb-name>
      example4/CoffeesHome
      example4/Coffees
      <ejb-class>example4/CoffeesEJB</ejb-class>

      <persistence-type>Bean</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      false

      <transaction-type>Container</transaction-type>
      <assembly-descriptor>
      <container-transaction>

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

      <trans-attribute>Supports</trans-attribute>
      </container-transaction>
      </assembly-descriptor>


      <resource-ref>
      <res-ref-name>jdbc/SQLServerPool</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
      </resource-ref>


      jboss.xml

      <ejb-name>CoffeesEJB</ejb-name>
      <jndi-name>ejb/Coffees</jndi-name>
      <reference-descriptor>
      <resource-description>
      <res-ref-name>jdbc/SQLServerPool</res-ref-name>
      <jndi-name>java:/SQLServerPool</jndi-name>
      </resource-description>
      </reference-descriptor>

      .....
      After deploying it,i used a client to test it:
      ...
      Object obj = ctx.lookup("ejb/Coffees");
      CoffeesHome coffeesHome = (CoffeesHome)
      PortableRemoteObject.narrow(obj, CoffeesHome.class);
      System.out.println("got home");
      Coffees coffees = coffeesHome.create(); <----report error here
      System.out.println("got remote interface");
      ...
      Coffees coffees = coffeesHome.create(); will report "id may not be null"

      I am a newbie for entity bean.Please give me some suggestion.

      Best Regards!

        • 1. Re: How to resolve the
          Gaute Lote Newbie

          An enity bean represents a persistent object, usually a
          row in a database. So the container and the db needs to know the primary key for this object. For an entitybean this means that the create method must at least have the pk as an argument: create(Integer primaryKey)

          • 2. Re: How to resolve the
            IPOz Newbie

            Thanks for your reply!
            Now i have added the parameter as below:
            home interface:
            ...
            public interface CoffeesHome extends javax.ejb.EJBHome
            {
            public Coffees create() throws RemoteException, CreateException;
            public Coffees create(String cofName) throws RemoteException, CreateException;
            }

            entity bean:
            ...
            public Object ejbCreate() throws CreateException
            {
            return null;
            }

            public Object ejbCreate(String cofName) throws CreateException
            {
            return null;
            }
            ....
            But it still reports " id may not be null"

            Best Regards!

            • 3. Re: How to resolve the
              Dain Sundstrom Master

              glote is wrong. The problem is you are not setting the primary key field. Therefore your pk is null. In ejb 1.1, you would have something like this:


              public String name;
              public Object ejbCreate(String name) throws CreateException {
              this.name = name;
              return null;
              }

              Make sure your declare name as the primary key field in your ejb-jar.xml file.