1 Reply Latest reply on Jan 4, 2003 12:20 PM by neitzel1

    Strange Problem with changing a CMP-Field

    neitzel1

      Hi everybody,

      I got a strange problem and don't know, where my mistake could be:

      I build up a entity Bean with a Container baased persitence field:

      public abstract Map getAttributes();
      public abstract void setAttributes(Map attribs);

      In the create-routine, I am puting a map inside that field or - if the given argument is null, I put there a new java.util.HashMap();

      I am adding Attributes there, so I build up a function inside the bean:

      public void setAttribute(String key, String obj) {
      Map m = getAttributes();
      if (m==null) {
      m = new HashMap();
      setAttributes(m); // could be left away
      }
      m.put(key,obj);
      setAttributes(m);
      }

      In ejbStore() and ejbLoad() I am just printing the contents of the stored map.

      Now I got this strange behavior:
      - initialisation works fine. I can give a Map with Attributes. For Example, I can give him the Attributes "testkey" -> "TestObject"

      When I am changing a keys, I get:

      Adding Key "a1" -> "a1obj"
      ejbLoad:
      "testkey" -> "TestObject"
      ejbStore:
      "testkey" -> "TestObject"
      "a1" -> "a1obj"

      This seems good, but now I am adding "b1" -> "b1obj"
      ejbLoad:
      "testkey" -> "TestObject"
      ejbStore:
      "testkey" -> "TestObject"
      "b1" -> "b1obj"

      So the stored Key "a1" -> "a1obj" is missing!

      When I am changing the Key:
      ejbLoad:
      "testkey" -> "TestObject"
      ejbStore:
      "testkey" -> "changed"

      But when I am queriing the Attributes I got:
      "testkey" -> "TestObject"
      instead of
      "testkey" -> "changed"
      again!

      Where can be my mistake? I am using
      jboss 3.0.4 with MySQL, I build up the datasource as shown in the jca-Example. Adding new Entities works fine. The initial values are stored as they should be, but I cannot change the Attributes!

      Maybe somebody could give me a clue, what I can do to fix this.

      With kind regards,

      Konrad

        • 1. SOLVED: Strange Problem with changing a CMP-Field
          neitzel1

          I think I got the solution.

          When I am changing the map (by adding / deleting / changing entries in the map), the map still is the same when looking from outside.

          So I think, that JBoss just checks, if the map has changed and thinks, that the map isn't changed. So the changed map isn't stored inside the database.

          My Solution:

          Map newMap = new HashMap();
          if (getAttriutes() != null) {
          newMap.putAll(getAttributes());
          }
          // Doing the changes
          setAttributes(newMap);

          With this code, JBoss sees, that there is a new Map now and the data is stored inside the database.

          With kind regards,

          Konrad