1 Reply Latest reply on May 10, 2005 2:54 PM by robr

    Simple entity bean will not insert data into a table.

    robr

      I am using Intellij 4.5.4 along with JBOSS 4.01sp1 with a JBOSS plugin. I am also using MSSQL.

      Everytime I try to create an instance of the entity bean I get the following error.


      Cannot insert the value NULL into column 'keyid', table 'WebQuoting.dbo.PropsTest'; column does not allow nulls. INSERT fails.


      I turned sql logging on in Log4j to see what sql is being generated. Here is the sql.


      INSERT INTO PropsTest (PropsTest.keyid, PropsTest.value) VALUES (?, ?)


      Apparently the code is using a prepared statement. It is obviously not getting the data set for the parameters.

      Here is the script that I used to create the table.

      CREATE TABLE [dbo].[PropsTest] (
       [keyid] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
       [value] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
      ) ON [PRIMARY]
      



      jbosscmp-jdbc.xml file using the PropsTableEJB enity

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE jbosscmp-jdbc PUBLIC "-//JBoss//DTD JBOSSCMP-JDBC 4.0//EN"
       "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_4_0.dtd">
      <jbosscmp-jdbc>
       <enterprise-beans>
       <entity>
       <ejb-name>PropsIdentityEJB</ejb-name>
       <datasource>java:/webquotes</datasource>
       <datasource-mapping>MS SQLSERVER</datasource-mapping>
       <pk-constraint>false</pk-constraint>
       <table-name>PropsTableIdentity</table-name>
       <cmp-field>
       <field-name>value</field-name>
       <column-name>PropsTableIdentity.value</column-name>
       </cmp-field>
       <unknown-pk>
       <unknown-pk-class>com.dynix.webquotes10.PropsIdentityPK</unknown-pk-class>
       <column-name>key</column-name>
       <jdbc-type>INTEGER</jdbc-type>
       <sql-type>INTEGER</sql-type>
       <auto-increment/>
       </unknown-pk>
       <entity-command name="mssql-fetch-key"/>
       </entity>
      
       <entity>
       <ejb-name>PropsTableEJB</ejb-name>
       <datasource>java:/webquotes</datasource>
       <datasource-mapping>MS SQLSERVER</datasource-mapping>
       <table-name>PropsTest</table-name>
       <cmp-field>
       <field-name>keyid</field-name>
       <column-name>PropsTest.keyid</column-name>
       </cmp-field>
       <cmp-field>
       <field-name>value</field-name>
       <column-name>PropsTest.value</column-name>
       </cmp-field>
       </entity>
      
       </enterprise-beans>
      </jbosscmp-jdbc>
      



      ejb-jar.xml -- the PropsTableEJB entity[/b/]

      <?xml version="1.0" encoding="UTF-8"?>
       <!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>
       <session>
       <ejb-name>HelloEJB</ejb-name>
       <home>com.dynix.webquote.HelloHome</home>
       <remote>com.dynix.webquote.Hello</remote>
       <ejb-class>com.dynix.webquote.HelloBean</ejb-class>
       <session-type>Stateless</session-type>
       <transaction-type>Container</transaction-type>
       </session>
       <entity>
       <description>test of entity</description>
       <display-name>PropsIdentityEJB</display-name>
       <ejb-name>PropsIdentityEJB</ejb-name>
       <home>com.dynix.webquotes10.PropsIdentityHome</home>
       <remote>com.dynix.webquotes10.PropsIdentity</remote>
       <ejb-class>com.dynix.webquotes10.PropsIdentityBean</ejb-class>
       <persistence-type>Container</persistence-type>
       <prim-key-class>com.dynix.webquotes10.PropsIdentityPK</prim-key-class>
       <reentrant>False</reentrant>
       <cmp-version>2.x</cmp-version>
       <abstract-schema-name>PropsIdentityEJB</abstract-schema-name>
       <cmp-field>
       <description>value type</description>
       <field-name>value</field-name>
       </cmp-field>
       </entity>
       
       <entity>
       <description>PropsTableEJB description</description>
       <display-name>PropsTableEJB</display-name>
       <ejb-name>PropsTableEJB</ejb-name>
       <home>com.dynix.webquotes10.PropsTableHome</home>
       <remote>com.dynix.webquotes10.PropsTable</remote>
       <ejb-class>com.dynix.webquotes10.PropsTableBean</ejb-class>
       <persistence-type>Container</persistence-type>
       <prim-key-class>java.lang.String</prim-key-class>
       <reentrant>False</reentrant>
       <cmp-version>2.x</cmp-version>
       <abstract-schema-name>PropsTableEJB</abstract-schema-name>
       <cmp-field>
       <description>Keyid col</description>
       <field-name>keyid</field-name>
       </cmp-field>
       <cmp-field>
       <description>value col</description>
       <field-name>value</field-name>
       </cmp-field>
       <primkey-field>keyid</primkey-field>
       </entity>
      
       </enterprise-beans>
       </ejb-jar>
       /code]
      
       jboss.xml file
      
      <?xml version="1.0"?>
      <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN"
       "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">
      <jboss>
       <!-- <security-domain>java:/jaas/webquotes</security-domain> -->
       <enterprise-beans>
       <session>
       <ejb-name>HelloEJB</ejb-name>
       <jndi-name>hello</jndi-name>
       </session>
       <entity>
       <ejb-name>PropsIdentityEJB</ejb-name>
       <jndi-name>PropsIdentity</jndi-name>
       </entity>
       <entity>
       <ejb-name>PropsTableEJB</ejb-name>
       <jndi-name>PropsTable</jndi-name>
       </entity>
       </enterprise-beans>
      </jboss>
      
      
      
      remote interface
      
      
      package com.dynix.webquotes10;
      
      import javax.ejb.EJBObject;
      import java.rmi.RemoteException;
      
      /**
       * Created by IntelliJ IDEA.
       * User: rb
       * Date: May 9, 2005
       * Time: 7:13:46 PM
       * To change this template use File | Settings | File Templates.
       */
      
      public interface PropsTable extends EJBObject {
      
       String getKeyid() throws RemoteException;
      
       void setKeyid(String keyid) throws RemoteException;
      
       String getValue() throws RemoteException;
      
       void setValue(String value) throws RemoteException;
      }
      
      
      
       bean file
      
      
      package com.dynix.webquotes10;
      
      import javax.ejb.*;
      
      /**
       * Created by IntelliJ IDEA.
       * User: rb
       * Date: May 9, 2005
       * Time: 7:13:45 PM
       * To change this template use File | Settings | File Templates.
       */
      
      public abstract class PropsTableBean implements EntityBean {
      
       public String key = null;
       public String value = null;
      
       public PropsTableBean() {
       }
      
       public void setEntityContext(EntityContext entityContext) throws EJBException {
       }
      
       public void unsetEntityContext() throws EJBException {
       }
      
       public String ejbCreate(String key, String value) throws CreateException {
       this.key = key;
       this.value = value;
       return null; //To change body of implemented methods use File | Settings | File Templates.
       }
      
       public void ejbPostCreate(String key, String value) throws CreateException {
       //To change body of implemented methods use File | Settings | File Templates.
       }
      
       public void ejbRemove() throws RemoveException, EJBException {
       }
      
       public void ejbActivate() throws EJBException {
       }
      
       public void ejbPassivate() throws EJBException {
       }
      
       public void ejbLoad() throws EJBException {
       }
      
       public void ejbStore() throws EJBException {
       }
      
       public abstract String getKeyid();
      
       public abstract void setKeyid(String keyid);
      
       public abstract String getValue();
      
       public abstract void setValue(String value);
      }
      
      
      
      
      Home file
      
      
      package com.dynix.webquotes10;
      
      import javax.ejb.EJBHome;
      import javax.ejb.FinderException;
      import javax.ejb.CreateException;
      import java.rmi.RemoteException;
      
      /**
       * Created by IntelliJ IDEA.
       * User: rb
       * Date: May 9, 2005
       * Time: 7:13:46 PM
       * To change this template use File | Settings | File Templates.
       */
      
      public interface PropsTableHome extends EJBHome {
       com.dynix.webquotes10.PropsTable findByPrimaryKey(String key) throws RemoteException, FinderException;
      
       PropsTable create(String key, String value) throws CreateException, RemoteException;
      }
      
      
      
      And here is the kitchen sink!!!@!!
      
      I don't know what I am missing here. Something to do with passing the data in so the insert doesn't use nulls for the data. This is my fourth day trying to get an extemely simple thing to work. I don't know if it is a configuration problem caused by IntellJ or if it is a JBOSS problem.
      
      Any help would be greatly appreciatted.
      
      Thanks.


        • 1. Problem solved.
          robr

          I had originaly learned how to write EJBs outside of a container using a tool called Blazix. I didn't realize the difference.

          In my first post I included the contents of a file called PropsTableBean.java. I will include it here with the changes. You can compare this copy to the previous copy to see what I did. Basicaly I used the setter methods to set the variables instead of having member variabes that I set.

          Just compare the ejbCreate methods to see the difference.

          package com.dynix.webquotes10;
          
          import javax.ejb.*;
          
          /**
           * Created by IntelliJ IDEA.
           * User: rb
           * Date: May 9, 2005
           * Time: 7:13:45 PM
           * To change this template use File | Settings | File Templates.
           */
          
          public abstract class PropsTableBean implements EntityBean {
          
           public PropsTableBean() {
           }
          
           public void setEntityContext(EntityContext entityContext) throws EJBException {
           }
          
           public void unsetEntityContext() throws EJBException {
           }
          
           public String ejbCreate(String key, String value) throws CreateException {
           setKeyid(key);
           setValue(value);
           return null; //To change body of implemented methods use File | Settings | File Templates.
           }
          
           public void ejbPostCreate(String key, String value) throws CreateException {
           //To change body of implemented methods use File | Settings | File Templates.
           }
          
           public void ejbRemove() throws RemoveException, EJBException {
           }
          
           public void ejbActivate() throws EJBException {
           }
          
           public void ejbPassivate() throws EJBException {
           }
          
           public void ejbLoad() throws EJBException {
           }
          
           public void ejbStore() throws EJBException {
           }
          
           public abstract String getKeyid();
          
           public abstract void setKeyid(String keyid);
          
           public abstract String getValue();
          
           public abstract void setValue(String value);
          }
          


          Hope this helps somebody else out.