Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer getting default value 0 where it required to be NULL in Java [duplicate]

Tags:

integer

jsf

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.

like image 645
Inayathulla Avatar asked Aug 23 '13 00:08

Inayathulla


1 Answers

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:

  • h:inputText which is bound to Integer property is submitting value 0 instead of null
  • Work around for faulty INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in Mojarra JSF 2.1

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.

like image 64
BalusC Avatar answered Nov 18 '22 17:11

BalusC