0 Replies Latest reply on Jul 28, 2005 4:07 AM by Sameer Saptarishi

    Could not create Entity exception

    Sameer Saptarishi Newbie

      Hello Friends,

      I deploy Hospital entity bean.The tables are created properly in the database.
      While entering a record using the CMP hospital bean I get the following error

      13:23:06,031 ERROR [Hospital] Could not create entity
      java.sql.SQLException: Column 'hospitalid' cannot be null
      at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921)
      at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570)
      at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1085)
      at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:670)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1159)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1076)
      at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1061)
      at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:316)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.executeInsert(JDBCAbstractCreateCommand.java:352)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCAbstractCreateCommand.performInsert(JDBCAbstractCreateCommand.java:308)

      Here is the code for my HospitalEJB


      public abstract class HospitalEJB implements EntityBean {

      private EntityContext context;
      public Long hospitalid;
      public String hospitalname;

      public void ejbLoad() throws EJBException, RemoteException {

      }
      public void ejbActivate() throws EJBException, RemoteException {

      }

      public Long ejbCreate(HospitalData hospitalData) throws CreateException {
      System.out.println("Came in ejbCreate method of HospitalEJB");
      Long myid = new Long(UniqueIdGenerator.getId());
      System.out.println("My id generated ====== " + myid);
      this.hospitalid = myid;
      System.out.println("My id generated ====== " + this.hospitalid);
      this.hospitalname = hospitalData.getHospitalname();
      System.out.println("Hospital Name " + hospitalData.getHospitalname());
      return myid;

      }

      public void ejbPostCreate(HospitalData hospitalData)
      throws CreateException, RemoteException {
      System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
      System.out.println("Came in EJB POSTCreate of HospitalEJB");
      System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

      }

      public void ejbRemove()
      throws RemoveException, RemoteException, EJBException {

      }

      public void setEntityContext(EntityContext context)
      throws EJBException, RemoteException {

      }

      public void unsetEntityContext() throws EJBException, RemoteException {

      }

      public void ejbPassivate() throws EJBException, RemoteException {

      }

      public void ejbStore() throws EJBException, RemoteException {

      }

      /**
      * @return
      */
      public abstract Long getHospitalid();

      /**
      * @return
      */
      public abstract String getHospitalname();

      /**
      * @param long1
      */
      public abstract void setHospitalid(Long long1);

      /**
      * @param string
      */
      public abstract void setHospitalname(String string);

      public HospitalData getHospitalData() {
      System.out.println("Came in get hospital data");
      System.out.println("In Get method of HospitalEJB" + this.hospitalid);
      System.out.println("In Get method of HospitalEJB" + this.hospitalname);

      HospitalData hospitalData = new HospitalData();
      hospitalData.setHospitalid(this.hospitalid);
      hospitalData.setHospitalname(this.hospitalname);
      System.out.println(
      "In Get method of HospitalEJB" + hospitalData.getHospitalid());
      System.out.println(
      "In Get method of HospitalEJB" + hospitalData.getHospitalname());
      return hospitalData;
      }

      public void setHospitalData(HospitalData hospitalData) {
      System.out.println(
      "In Set method of HospitalEJB" + hospitalData.getHospitalname());
      this.hospitalid = hospitalData.getHospitalid();
      this.hospitalname = hospitalData.getHospitalname();
      }

      }

      Here is my ejb-jar.xml
      <?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>

      <ejb-name>UserManager</ejb-name>
      com.cygnet.medina.UserManager.UserManagerHome
      com.cygnet.medina.UserManager.UserManager
      <ejb-class>com.cygnet.medina.UserManager.UserManagerEJB</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>


      <assembly-descriptor>
      <container-transaction>

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

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



      <ejb-name>HospitalManager</ejb-name>
      com.cygnet.medina.HospitalManager.HospitalManagerHome
      com.cygnet.medina.HospitalManager.HospitalManager
      <ejb-class>com.cygnet.medina.HospitalManager.HospitalManagerEJB</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>


      <assembly-descriptor>
      <container-transaction>

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

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


      <ejb-name>AnesthesiaUser</ejb-name>
      com.cygnet.medina.user.AnesthesiaUserHome
      com.cygnet.medina.user.AnesthesiaUser
      <ejb-class>com.cygnet.medina.user.AnesthesiaUserEJB</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Long</prim-key-class>
      False
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>anesthesiauser</abstract-schema-name>
      <cmp-field>
      <field-name>userid</field-name>
      <column-name>userid</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>username</field-name>
      <column-name>username</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>password</field-name>
      <column-name>password</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>usertype</field-name>
      <column-name>usertype</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>createddate</field-name>
      <column-name>createddate</column-name>
      </cmp-field>
      <cmp-field>
      <field-name>updateddate</field-name>
      <column-name>updateddate</column-name>
      </cmp-field>
      <primkey-field>userid</primkey-field>

      <query-method>
      <method-name>findByUsername</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>
      </query-method>
      <ejb-ql>SELECT OBJECT(t) FROM anesthesiauser AS t WHERE t.username = ?1</ejb-ql>



      <assembly-descriptor>
      <container-transaction>

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

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>
      <!-- Added -->


      <ejb-name>Hospital</ejb-name>
      com.cygnet.medina.Hospital.HospitalHome
      com.cygnet.medina.Hospital.Hospital
      <ejb-class>com.cygnet.medina.Hospital.HospitalEJB</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Long</prim-key-class>
      False
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>hospital</abstract-schema-name>
      <cmp-field>
      <field-name>hospitalid</field-name>
      </cmp-field>
      <cmp-field>
      <field-name>hospitalname</field-name>
      </cmp-field>
      <primkey-field>hospitalid</primkey-field>

      <assembly-descriptor>
      <container-transaction>

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

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>
      <!-- End -->

      </enterprise-beans>
      </ejb-jar>

      Here is my jbosscmp-jdbc.xml

      <?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">

      <!-- ===================================================================== -->
      <!-- -->
      <!-- Standard JBossCMP-JDBC Configuration -->
      <!-- -->
      <!-- ===================================================================== -->

      <!-- $Id: standardjbosscmp-jdbc.xml,v 1.84.2.6 2005/01/26 18:51:35 starksm Exp $ -->

      <jbosscmp-jdbc>


      java:/MySqlDS
      <datasource-mapping>mySQL</datasource-mapping>
      <create-table>true</create-table>
      <remove-table>true</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>


      <enterprise-beans>

      <ejb-name>AnesthesiaUser</ejb-name>

      <table-name>anesthesiauser</table-name>


      <!-- Added -->

      <ejb-name>Hospital</ejb-name>
      <table-name>hospital</table-name>


      <!-- End -->
      </enterprise-beans>

      </jbosscmp-jdbc>


      I am using JBoss 4.0.2 and MySQL 4.1.13