3 Replies Latest reply on Jan 11, 2002 1:22 PM by Tom Mahaffey

    Handling null values in CMP 2.0

    Ralf Ebert Newbie

      Hello,

      I just tried the CMP 2.0 features of the jBoss 3.0 Alpha and I got to a question I couldn't answer myself. As the database gives back null when the field in the database is empty, you have the problem of handling these null's when using the data, for example when showing on the client. I don't know, but I don't want to do this on the client, so I want my entity bean to give me back an empty string when the value is null. So I set my table structure to NOT NULL DEFAULT ''. Hmm, a bit database overhead, but I don't mind. But CMP does. It inserts with VALUES (NULL). And MySQL doesn't come to the idea to use the default value instead, so I get an SQLException. Okay, so I tried to set the values in the create-Method, so I call setXXX(""). This works, but it looks ugly. Is there any better way for this ?
      Thanks for your help,

      Regards,
      Ralf Ebert

        • 1. Re: Handling null values in CMP 2.0
          Tom Mahaffey Newbie

          I don't use MySQL, but I'm wondering if NOT NULL DEFAULT '' means that it will actually insert the empty string if it gets a NULL. In other words, if you insert with VALUES(NULL) from a mysql client, does it complain the same way? or does it go ahead and insert the empty string?

          Maybe MySQL only used the default empty string when you *don't* specify a value?

          • 2. Re: Handling null values in CMP 2.0
            Ralf Ebert Newbie

            No, I get an error as well when I try it from the mysql command line. I also thought that "values (null)" should make mySQL inserting the default value. So it seems to be a bug (or a feature) of MySQL. I will try it with the newest release of MySQL, maybe it's really a bug.
            But if it's not, is there a better way to code around this except using setxxx statements in the create-method of my entity bean ?

            • 3. Re: Handling null values in CMP 2.0
              Tom Mahaffey Newbie

              IMHO, I don't think it's a bug...but remember I'm not a MySQL user.

              Looks to me like the NOT NULL part is a constraint and the DEFAULT '' part is a trigger-ish thing which says "if i don't get a VALUE for this column, make it the empty string." In MySQL's view, you indeed gave it a value, the value just happened to be NULL.

              Although using the callback methods may look weird to you now, that's what they're there for.