Uploaded image for project: 'Application Server 3  4  5 and 6'
  1. Application Server 3 4 5 and 6
  2. JBAS-2270

Cannot set cmp-field to null for entities obtained via a container managed relationship (CMR)

XMLWordPrintable

      Fields of a CMP entity bean cannot be set to null - setting to a value works.

      To my understanding, the bug is introduced by the fact that the entity bean is not loaded at the time values are set to it. If the field is set to some value (a setAttr(...) method is called on a CMP entity bean), the old value == null and the current value is set to the new value. If the new value is "null", then the comparison in JDBCTypeFactory.EQUALS.isStateValid(Object state, Object fieldValue) fails to mark the field as dirty.

      Object state --> the old state of the field
      Object fieldValue --> the new value of the field

      If the CMP entity bean was not accessed before (e.g. getAttr() was not called), both state and fieldValue are null if setAttr(...) was called with "null" as argument. Hence the comparison "state == null ? fieldValue == null : state.equals(fieldValue)" returns true. Consequently, the field is not marked as dirty and the current probably non-null value remains in the database.

      Example: A CMP bean A has id=1, attr1="x", attr2="y", and attr3="z" stored in the database. If I update the bean by calling A.setAttr1("a"), A.setAttr2("b"), and A.setAttr3(null), this results in having the values id=1, attr1="a", attr2="b", and attr3="z" in the database.

      A field can be set to null by the following workaround: Call getAttr() before you call setAttr(null).

              olubyans@redhat.com Alexey Loubyansky
              lorenzf Lorenz Froihofer (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: