2 Replies Latest reply on May 7, 2004 9:01 AM by roger_cmu

    Updating entity bean fields using BeanUtils

    roger_cmu

      I have an entity bean with the following three methods.

       public abstract Integer getProductid();
       public abstract void setProductid(Integer productid);
       public abstract String getProduct_desc();
       public abstract void setProduct_desc(String desc);
       public abstract String getProduct_info();
       public abstract void setProduct_info(String info);
      


      Let's look at method update:
       public void update(ProductDetails productDetails) {
       logger.info("update");
       try {
       BeanUtils.copyProperties(this, productDetails);
       }catch (java.lang.reflect.InvocationTargetException e) {
       logger.warn("InvocationTargetException", e);
       }catch (IllegalAccessException e) {
       logger.warn("Illegal Access exception", e);
       }
       }
      


      Let's look at ProductDetails
       private int productid;
       private int product_desc;
       private int product_info;
      .... getter and setters for all the three variables declared above.
      


      As you noticed I am using BeanUtils.copyProperties(this, productDetails); code to copy all the values from productDetails to update the entity bean. I get an error when I execute update.
      java.lang.IllegalStateException: A CMP field that is a member of the primary key can only be set in ejbCreate


      This is a valide error. What's fix: I thought of having two value objects for each entity bean. One class will have all the fields except the primary key field. So my productDetails class will drop productid from the above mentioned code.

      Next I'll extend produceDetails class to produceDetailsID which will just have productid field along with getter and setter.

      Next my update code will use productDetailsID instead of productDetails to update the bean.

      Is there a better way to handle this case. This is my first ejb project. How do most of developers update the entity bean fields. Some of my beans have lots of fields.

      BeanUtils class is available at org.apache.commons.beanutils.BeanUtils.

      Thanks.

      - Roger