CMP 2.0 field validation

Discussions

EJB design: CMP 2.0 field validation

  1. CMP 2.0 field validation (1 messages)

    I like that with CMP 2.0, I don't have to define entity bean fields, nor the bodies of their accessor methods--just declare the abstract accessors, and I'm done.

    But it does create a problem: how can I introduce field validation logic into field setters, where it properly belongs, when the setter is abstract?

    For example, how can I as a bean developer enforce that the IQ field of my Person entity is always a non-negative number?
  2. CMP 2.0 field validation[ Go to top ]

    I can see two possibilities in your case:

      1) you should have a session facade between the client and the entity beans and the validation may be implemented inside the session beans.

      2) if you really want to implement the validation logic inside the entity bean you could implement for each set method another method which validates the data and then calls the abstract method. Let me give you an example:

      public abstract void setIQ(int iq);

      public void validateAndSetIQ(int iq){
        if(iq > 0) setIQ(iq);
      }

      and from your client/session bean if you want to use the validation you can call the validateAndSetIQ() method.