1 Reply Latest reply on Sep 26, 2005 10:44 AM by Oliver Roell

    Problems with a primary-key from an entity-bean

    Oliver Roell Newbie

      Hi,

      I am using JBoss 4.0.2 with the embedded HSQLDB, and I have written an entity-bean with a specific Primary-Key-Compound-class, and it doesn't work. All my other entity-beans with Integer or String as PK work well.

      This is my PK-class:

      public class RecipePosPK {
      public int recipeId;
      public int position;

      public RecipePosPK() { }

      public boolean equals(Object obj) {
      if (this.getClass().equals(obj.getClass())) {
      RecipePosPK other = (RecipePosPK) obj;
      return (this.recipeId == other.recipeId
      && this.position == other.position);
      }
      return false;
      }

      public int hashCode() {
      return(("" + recipeId + position).hashCode());
      }
      }

      And this is an excerpt from my Entity-Bean-class:

      /**
      * Create method
      * @ejb.create-method view-type = "local"
      */
      public RecipePosPK ejbCreate(int recipeID, RecipePos pos)
      throws javax.ejb.CreateException {

      RecipePosPK key = new RecipePosPK();
      key.recipeId = recipeID;
      key.position = pos.getPos();
      setRecipePosPK(key);

      setSide(pos.getSide());
      setWorkDesc(pos.getWorkDesc());
      setMachine(pos.getMachine());
      setCount(pos.getCount());
      setColor(pos.getColor());
      setRecipeDesc(pos.getRecipeDesc());
      setComment(pos.getComment());
      return null;
      }

      /**
      * Post Create method
      */
      public void ejbPostCreate(int recipeID, RecipePos pos)
      throws javax.ejb.CreateException {
      }

      /**
      * Getter for CMP Field recipePosPK
      *
      * @ejb.pk-field
      * @ejb.persistent-field
      * @ejb.interface-method view-type="local"
      */
      public abstract entitybeans.interfaces.RecipePosPK getRecipePosPK();
      public abstract void setRecipePosPK(entitybeans.interfaces.RecipePosPK value);


      When I deploy this entity-bean in JBoss, the following exception was thrown from the app-server:

      Starting failed jboss.j2ee:jndiName=RecipeDescLocal,service=EJB
      org.jboss.deployment.DeploymentException: Error while creating table RECIPEPOS; - nested throwable:
      (java.sql.SQLException: Unexpected token: ) in statement
      [CREATE TABLE RECIPEPOS (recipePosPK VARCHAR(100), side VARCHAR(100), workDesc VARCHAR(100), machine VARCHAR(100), count VARCHAR(100), color VARCHAR(100), recipeDesc VARCHAR(100), comment VARCHAR(100), CONSTRAINT PK_RECIPEPOS PRIMARY KEY ()])


      I am not sure whether the PK-class is responsible for the error, but I think so, because it's the only entity-bean that does not work, and it's my first entity-bean with a PK-compound-class, so I think, that the PK-class is the cause of the error.

      Could anyone please check my code and give me an advice?

      I don't know whether my handling of the PK-compound-class is correct.

      Thanks in advance.

      Regards,
      Oliver

        • 1. Re: Problems with a primary-key from an entity-bean
          Oliver Roell Newbie

          I have solved the problem. The spec says about compound primary keys:

          The names of the fields in the primary key class must be a subset of the names of the container-managed fields.

          I have missed to create the primary key class fields as container-managed fields.

          Furthermore I have figured out, that JBoss or HSQLDB or whoever do not like primary key class fields with the name position. After I have changed the name position to another name, the deployment was successful. I don't know why the name position breaks my deployment, but now it works, I hope.