11

My requirement looks simple, but I feel I'm doing something wrong in code.

All I need is to insert "null" in Oracle Database when nothing is passed from InputText field of UI.

PaymantSpecFormPage.xhtml

<h:inputText id="startWeek" value="#{flowScope.paymentSpecVO.paymStartWk}" styleClass="inputTypeForm" maxlength="4"/>

PaymentSpecVO.java

public class PaymentSpecVO extends BaseVO<PaymentSpec> {
   private Integer paymStartWk;

   public Integer getPaymStartWk() {
      return paymStartWk;
   }
   public void setPaymStartWk(Integer paymStartWk) {
      this.paymStartWk = paymStartWk;
   }
}

And in my DB Table

COLUMN_NAME      DATA_TYPE    NULLABLE    DEFAULT_VALUE
------------     ---------    --------    -------------
PAYM_START_WK    NUMBER(4,0)    Yes             null

And when I enter nothing in inputText, then instead of null, 0 is getting entered in Table, which has different meaning in my business logic, please help me to do necessary code changes.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Inayathulla
  • 761
  • 2
  • 8
  • 21

3 Answers3

17

And when I enter nothing in inputText, then instead of null, 0 is getting entered in table

This problem is recognizable as Tomcat's EL parser not taking into account that you're using Integer instead of int. You need to add the following VM argument to Tomcat startup options:

-Dorg.apache.el.parser.COERCE_TO_ZERO=false

See also:


It solved my half of problem, and now its storing null values, but while on retrieve it shows 0 again

This problem is recognizable as JDBC under the covers using ResultSet#getInt() instead of ResultSet#getObject(). This is however essentially a different problem than the first one. You didn't tell anything about how your database access layer is setup, so it's hard to point out a concrete solution.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have used -Dorg.apache.el.parser.COERCE_TO_ZERO=false, as an argument and my business logic are making use of Integer datatype rather than int. I'm using Spring Data JPA to persist entries. @Transactional(readOnly = false) public PaymentSpec save(PaymentSpec PaymentSpec); – Inayathulla Aug 23 '13 at 11:30
  • Sorry, I don't do Spring. You'd better ask a separate question why a `NUMBER` field with a `null` value is obtained as `0` from Spring Data JPA. – BalusC Aug 23 '13 at 11:32
  • Thank you, I love to read your comments :) – Inayathulla Aug 23 '13 at 11:36
  • @BalusC this issue actually exists in JBoss Wildfly 8.2.0 final. I have a `MessageBean` with an `Integer` variable that is null but when doing `#{not empty value}` the component gets rendered. Is there a way of treating `null` `Integer` as null in JSF EL? – Buhake Sindi May 29 '15 at 00:02
-1

I don't know how you are reading that xhtml. At java layer, you can override either the constructor of PaymentSpecVO or the setter for paymStartWk

Crickcoder
  • 2,135
  • 4
  • 22
  • 36
-1

Try this

    public class PaymentSpecVO extends BaseVO<PaymentSpec> {
    private String paymStartWk;

    public String getPaymStartWk() {
    return paymStartWk;
    }
    public void setPaymStartWk(Integer paymStartWk) {
    this.paymStartWk = paymStartWk+"";
  }
}

But then you might be needing a lot of parsing there.

Edit*

If you enter nothing to your "input Text" the passed value is null but since you're catching it with an Integer, it interprets null to 0. So when you save it, even after manipulating methods and variables to null , it will store "0" , because basically thats the default value.

Another solution I think is making a custom class that stores Integers

Example :

    public class IntegerVal extends Number {
    private Max_val = 10000000;
    private Min_val = -10000000;
    private String default = null;
    }

you can do whatever implementations you want ,, like returning null,

You can use that as a substitute to the Integer Class you are using , if you dont wanna use String

misserandety
  • 122
  • 1
  • 4
  • 13