Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy a NUMBER field with a null value is obtained as 0 from Spring Data JPA
    text
    copied!<p>I have a problem with retrieving <code>Integer</code> values from database. I am using Spring Data JPA to interact with the database. For a <code>NUMBER</code> column which is nullable, the NULL value from Database (Oracle) is getting back as <code>0</code>, instead of <code>null</code>.</p> <p>Here is the code:</p> <p><code>PaymentSpecVO.java</code>:</p> <pre><code>public class PaymentSpecVO extends BaseVO&lt;PaymentSpec&gt; { private Integer paymStartWk; public Integer getPaymStartWk() { return paymStartWk; } public void setPaymStartWk(Integer paymStartWk) { this.paymStartWk = paymStartWk; } } </code></pre> <p><code>PaymentSpecUIService.java</code></p> <pre><code>public class PaymentSpecUIService implements IPaymentSpecUIService { @Autowired protected IPaymentSpecService paymentSpecService; public void savePaymentSpec(PaymentSpecVO paymentSpecVO) { //some logic paymentSpec = paymentSpecService.save(paymentSpec); // transform the saved entity back to VO object //Here All my VO objects extends BaseVO paymentSpecVO.toDTO(paymentSpec); } public PaymentSpecVO findPaymentSpecByKey(PaymentSpecId key) { //Here during retrival It uses findByKey(key); PaymentSpec paymentSpec = paymentSpecService.findByKey(key); PaymentSpecVO paymentSpecVO = new PaymentSpecVO(); if (paymentSpec != null) { paymentSpecVO.toDTO(paymentSpec); return paymentSpecVO; } } } </code></pre> <p><b>BaseVO.java</b></p> <pre><code>public abstract class BaseVO&lt;E extends Object&gt; implements Serializable { private Class&lt;E&gt; entity; protected BaseVO(Class&lt;E&gt; entity) { this.entity = entity; ConvertUtils.register(dateConverter, Date.class); } public Object toDTO(E entity) { try { copyEntityToValueObjectProperties(entity, this); } catch (Exception ex) { ex.printStackTrace(); } return this; } /** * Copies the properties from Entity object to Value object. * * */ public void copyEntityToValueObjectProperties(Object entityObject, Object valueObject) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,InstantiationException, SecurityException, NoSuchFieldException { // Get the list of fields from source objects Field[] entityObjectFields = entityObject.getClass().getDeclaredFields(); // Reference of BaseEntity class type Class&lt;?&gt; entityType = BaseEntity.class; Class&lt;?&gt; collType = Collection.class; // Iteration of entity properties for (Field entityField : entityObjectFields) { try{ // // Checks whether the entity field is an object and is of type // BaseEntity class. // if (!entityType.isAssignableFrom(entityField.getType()) &amp;&amp; !collType.isAssignableFrom(entityField.getType()) &amp;&amp; !Modifier.isFinal(entityField.getModifiers()) &amp;&amp; !Modifier.isStatic(entityField.getModifiers())) { BeanUtils.setProperty(valueObject, entityField.getName(), BeanUtils.getProperty(entityObject, entityField.getName())); } } catch(Exception e){ continue; } } } </code></pre> <p>}</p> <p><b>PaymentSpecId.java</b></p> <pre><code>public class PaymentSpecId implements BaseEntity { private String contractId; private String sectionId; private String id; public PaymentSpecId() { } public PaymentSpecId(String id, String contractId, String sectionId){ this.id = id; this.contractId = contractId; this.sectionId = sectionId; } @Column(name = "ID", nullable = false, length = 2) public String getId() { return this.id; } public void setId(String id) { this.id = id; } } </code></pre> <p><code>IPaymentSpecService.java</code></p> <pre><code>@Transactional(readOnly = true) public interface IPaymentSpecService { @Transactional(readOnly = false) public PaymentSpec save(PaymentSpec PaymentSpec); } </code></pre> <p>My DB Table</p> <pre><code>COLUMN_NAME DATA_TYPE NULLABLE DEFAULT_VALUE ------------ --------- -------- ------------- PAYM_START_WK NUMBER(4,0) Yes null </code></pre> <p>And when I click on edit link from UI i.e., JSF page, its passes paymentID to findPaymentSpecByKey(PaymentSpecId key) method of PaymentSpecUIService.java by using backing beans PaymentSpecVO.java and In popup it will display paymStartWK value in inputText component as 0, though the data in DB is stored as NULL</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload