5 Replies Latest reply on Sep 23, 2002 3:11 PM by Dzidas

    Error checking if entity exists

    Dzidas Newbie

      Hello,
      I have CMP bean. When I used Integer class for primary key, my bean was working. When I create primary key class:

      package count;
      import java.io.Serializable;

      public class CountPK implements Serializable {
      public CountPK()
      {
      System.out.println(this.id + "konst1");
      }
      /* public CountPK(Integer param)
      {
      this.id = param;
      System.out.println(this.id+ "konst2");
      }
      */
      public Integer getId()
      {
      return id;
      }
      public boolean equals(Object o)
      {
      System.out.println(this.id + "equals");
      if (o instanceof CountPK)
      {
      CountPK otherKey = (CountPK)o;
      return (id == otherKey.id);
      } else return false;
      }

      public int hashCode()
      {
      System.out.println(this.id + "hash");
      return id.hashCode();
      }


      public Integer id;
      }
      Now my bean deploy succesful, but when I try use from client I get this error:
      2002-09-22 16:20:55,910 INFO [STDOUT] ejbCreate9kitas: 9
      2002-09-22 16:20:55,910 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.CountBean] Create: pk=null
      2002-09-22 16:20:55,910 DEBUG [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.CountBean] Executing SQL: SELECT COUNT(*) FROM count WHERE
      2002-09-22 16:20:55,920 ERROR [org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.CountBean] Error checking if entity exists
      java.sql.SQLException: ERROR: parser: parse error at or near ""

      at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:94)
      at org.postgresql.Connection.ExecSQL(Connection.java:398)
      at org.postgresql.jdbc2.Statement.execute(Statement.java:130)
      at org.postgresql.jdbc2.Statement.executeQuery(Statement.java:54)
      at org.postgresql.jdbc2.PreparedStatement.executeQuery(PreparedStatement.java:99)
      at org.jboss.resource.adapter.jdbc.local.LocalPreparedStatement.executeQuery(LocalPreparedStatement.java:289)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.entityExists(JDBCCreateEntityCommand.java:154)
      at org.jboss.ejb.plugins.cmp.jdbc.JDBCCreateEntityCommand.execute(JDBCCreateEntityCommand.java:127)

      ejbCreate metod is:
      public java.lang.Object ejbCreate(Integer id, Integer quantity)
      throws CreateException, EJBException, SQLException {

      setId(id);
      setQuantity(quantity);
      System.out.println("ejbCreate" + id + "kitas: " + quantity);
      return null;
      }

      and deployment descriptor:
      <?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>
      <display-name>Count Entity Bean</display-name>
      <enterprise-beans>


      Count Entity Bean
      <ejb-name>CountBean</ejb-name>
      count.CountHome
      count.Count
      <ejb-class>count.CountBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.Object</prim-key-class>
      False
      <cmp-version>2.x</cmp-version>

      <cmp-field><field-name>id</field-name></cmp-field>
      <cmp-field><field-name>quantity</field-name></cmp-field>
      <abstract-schema-name>CountBean</abstract-schema-name>

      <!--<primkey-field>id</primkey-field>-->


      </enterprise-beans>

      <assembly-descriptor>
      <container-transaction>

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

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

      where error can be ?
      thanks

        • 1. Re:  Error checking if entity exists
          Alexey Loubyansky Master

          - how do you set the value of id attribute in CountPK?
          - if you want primaty key of type CountPK, the bean must have a cmp field of type CountPK (not Integer)
          - prim-key-class must have a value of primary key's type (my.package.CountPK). java.lang.Object declares unknown primary key that is supported only in 4.0 version currently.

          alex

          • 2. Re:  Error checking if entity exists
            Tung Dang Novice

            Hi,

            You have to map the pk class in your deployment descriptor like this:
            <prim-key-class>count.CountPK</prim-key-class>

            Be sure that your ejbCreate in your bean returns this primary key class, as well, e.g:

            public CountPK ejbCreate(Integer i) {... }

            Hope this helps.

            • 3. Re:  Error checking if entity exists
              Dzidas Newbie

              hello,
              Are you sure?
              I use this rules from sun:
              In the deployment descriptor, the primary key class is defined as a java.lang.Object. The primary key field is not specified.
              In the home interface, the argument of the findByPrimaryKey method must be a java.lang.Object.
              In the entity bean class, the return type of the ejbCreate method must be a java.lang.Object.
              link:
              http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/CMP8.html#73119

              If I change from java.lang.Object to CountPK, then I get error when I deploy our bean.
              alex:
              - how do you set the value of id attribute in CountPK?
              I have records in database and tray get this records.

              - if you want primaty key of type CountPK, the bean must have a cmp field of type CountPK (not Integer)
              Yes, my bean return type is java.lang.Object

              prim-key-class must have a value of primary key's type (my.package.CountPK).
              Sorry, but I don't understand. My PK class must have our type inside class???

              Maybe you can send to my our working bean?

              • 4. Re:  Error checking if entity exists
                Alexey Loubyansky Master

                I was wrong when writting that the bean must have a cmp-field of type CountPK.
                But prim-key-class must be set to count.CountPK and not to java.lang.Object!
                ejbCreate method must return count.CountPK either. And findByPrimaryKey method must take the parameter of type CountPK. Not Object!

                The 'Generating Primary Key Values' is a special case. Read once again. This is for container generated keys. Refer also to paragraph 10.8.3 of EJB2.0 spec.

                alex

                • 5. Re:  Error checking if entity exists
                  Dzidas Newbie

                  Re,
                  big thanks alex. You was right, I change deployment desc., create class and home object. Now my bean deploy properly and I can use from servlet.

                  thanks for all.

                  kafka