title: How to handle database null values in Hibernate Application?
body: <p>I am getting " org.hibernate.PropertyAccessException" due to null values in my database table. How to handle the exception?</p>
  
  <p>My files are</p>
  
  <p>FetchTest.java</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>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();
   
  }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>Employee.java</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>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;
   }
  
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>hibernate.cfg.xml</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  
   &lt;session-factory&gt;
    &lt;property name="hbm2ddl.auto"&gt;update&lt;/property&gt;
    &lt;property name="dialect"&gt;org.hibernate.dialect.Oracle9Dialect
        &lt;/property&gt;
    &lt;property name="connection.url"&gt;someValidurl&lt;/property&gt;
    &lt;property name="connection.username"&gt;username&lt;/property&gt;
    &lt;property name="connection.password"&gt;password&lt;/property&gt;
    &lt;property name="connection.driver_class"&gt;oracle.jdbc.driver.OracleDriver&lt;/property&gt;
    &lt;property name="hibernate.show_sql"&gt;true&lt;/property&gt;
    &lt;property name="cache.provider_class"&gt;org.hibernate.cache.EhCacheProvider&lt;/property&gt;
    &lt;property name="hibernate.cache.use_second_level_cache"&gt;true&lt;/property&gt;
    &lt;mapping resource="employee.hbm.xml"&gt;&lt;/mapping&gt;
   &lt;/session-factory&gt;
  
  &lt;/hibernate-configuration&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>employee.hbm.xml</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  &lt;hibernate-mapping&gt;
   &lt;!-- &lt;class name="com.javatpoint.mypackage.Employee" table="employee"&gt; &lt;id 
    name="empid"&gt; &lt;generator class="assigned"&gt;&lt;/generator&gt; &lt;/id&gt; &lt;property name="firstName"&gt;&lt;/property&gt; 
    &lt;property name="lastName"&gt;&lt;/property&gt; &lt;/class&gt; --&gt;
   &lt;class name="com.raj.java.hiberanteDemos.Employee" table="emp"&gt;
    &lt;id name="empno" type="int" column="EMPNO"&gt;
     &lt;generator class="increment" /&gt;
    &lt;/id&gt;
    &lt;property name="ename" type="java.lang.String" /&gt;
    &lt;property name="mgr" type="int" /&gt;
    &lt;property name="deptnumber" type="int" column="deptno" /&gt;
    &lt;property name="job" type="java.lang.String" /&gt;
    &lt;property name="sal" type="double" /&gt;
    &lt;property name="comm" type="double" /&gt;
    &lt;property name="hiredate" type="java.sql.Date" /&gt;
  
   &lt;/class&gt;
  &lt;/hibernate-mapping&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>When I ran this application I am getting below error message.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>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(&lt;generated&gt;)
   at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:212)
   ... 19 more
  Caused by: java.lang.NullPointerException
   ... 21 more</code></pre>
  </div>
  </div>
  </p>
  
  <p>my emp table data </p>
  
  <p><img src="https://i.stack.imgur.com/ZovXi.jpg" alt="EmpData"></p>
  
  <p>when I update all the null values with 0 or some other values its working fine. </p>
  
  <p>Please help me in solving the error without updating null values in database table.</p>
  
  <p>Thanks in Advance,
  Raj</p>