Raj Raj - 3 months ago 8
Java Question

How to handle database null values in Hibernate Application?

I am getting " org.hibernate.PropertyAccessException" due to null values in my database table. How to handle the exception?

My files are

FetchTest.java



package com.raj.java.hiberanteDemos;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class FetchTest {
public static void main(String[] args) {
Configuration cfg=new Configuration().configure("hibernate.cfg.xml");
SessionFactory factory=cfg.buildSessionFactory();

Session session1=factory.openSession();
Employee emp1=(Employee)session1.get(Employee.class,7839);
System.out.println(emp1.getEmpno()+" "+emp1.getEname()+" "+emp1.getSal());
session1.close();

Session session2=factory.openSession();
Employee emp2=(Employee)session2.load(Employee.class,7839);
System.out.println(emp2.getEmpno()+" "+emp2.getEname()+" "+emp2.getSal());
session2.close();

}
}





Employee.java



package com.raj.java.hiberanteDemos;

import java.sql.Date;


class Employee {
private int empno, mgr, deptnumber;
private String ename, job;
private double sal, comm;
private Date hiredate;

public int getEmpno() {
return empno;
}

public void setEmpno(int empno) {
this.empno = empno;
}

public int getMgr() {
return mgr;
}

public void setMgr(int mgr) {
this.mgr = mgr;
}

public int getDeptnumber() {
return deptnumber;
}

public void setDeptnumber(int deptnumber) {
this.deptnumber = deptnumber;
}

public String getJob() {
return job;
}

public void setJob(String job) {
this.job = job;
}

public double getComm() {
return comm;
}

public void setComm(double comm) {
this.comm = comm;
}

public Date getHiredate() {
return hiredate;
}

public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}

public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename;
}

public double getSal() {
return sal;
}

public void setSal(double sal) {
this.sal = sal;
}

}





hibernate.cfg.xml



<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">someValidurl</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.show_sql">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<mapping resource="employee.hbm.xml"></mapping>
</session-factory>

</hibernate-configuration>





employee.hbm.xml



<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- <class name="com.javatpoint.mypackage.Employee" table="employee"> <id
name="empid"> <generator class="assigned"></generator> </id> <property name="firstName"></property>
<property name="lastName"></property> </class> -->
<class name="com.raj.java.hiberanteDemos.Employee" table="emp">
<id name="empno" type="int" column="EMPNO">
<generator class="increment" />
</id>
<property name="ename" type="java.lang.String" />
<property name="mgr" type="int" />
<property name="deptnumber" type="int" column="deptno" />
<property name="job" type="java.lang.String" />
<property name="sal" type="double" />
<property name="comm" type="double" />
<property name="hiredate" type="java.sql.Date" />

</class>
</hibernate-mapping>





When I ran this application I am getting below error message.



log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select employee0_.EMPNO as EMPNO0_0_, employee0_.ename as ename0_0_, employee0_.mgr as mgr0_0_, employee0_.deptno as deptno0_0_, employee0_.job as job0_0_, employee0_.sal as sal0_0_, employee0_.comm as comm0_0_, employee0_.hiredate as hiredate0_0_ from emp employee0_ where employee0_.EMPNO=?
Exception in thread "main" org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.raj.java.hiberanteDemos.Employee.setMgr
at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:215)
at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:185)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3232)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:47)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:41)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2730)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:177)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:799)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:792)
at com.raj.java.hiberanteDemos.FetchTest.main(FetchTest.java:13)
Caused by: net.sf.cglib.beans.BulkBeanException
at com.raj.java.hiberanteDemos.Employee$$BulkBeanByCGLIB$$142cfd75.setPropertyValues(<generated>)
at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:212)
... 19 more
Caused by: java.lang.NullPointerException
... 21 more





my emp table data

EmpData

when I update all the null values with 0 or some other values its working fine.

Please help me in solving the error without updating null values in database table.

Thanks in Advance,
Raj

Answer

I would personally recommend to actually "clean" the database values, maybe setting the column not nullable.

But if this can't be done, what you can do is modify your setters, so that it checks for null

EDIT: since double is a primitive, try to wrap it to a Double object

public void setComm(double comm) {
    if(null != Double.valueOf(comm)){
        this.comm = comm;

    }else{
        this.comm = 0;
    }
}

hope this helps

Comments