java - How to handle database null values in Hibernate Application? -
i getting " org.hibernate.propertyaccessexception" due null values in database table. how handle 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 ran application getting below error message.
log4j:warn no appenders found logger (org.hibernate.cfg.environment). log4j:warn please initialize log4j system properly. hibernate: select employee0_.empno empno0_0_, employee0_.ename ename0_0_, employee0_.mgr mgr0_0_, employee0_.deptno deptno0_0_, employee0_.job job0_0_, employee0_.sal sal0_0_, employee0_.comm comm0_0_, employee0_.hiredate hiredate0_0_ emp employee0_ employee0_.empno=? exception in thread "main" org.hibernate.propertyaccessexception: exception setting property value cglib (set hibernate.cglib.use_reflection_optimizer=false 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
when update null values 0 or other values working fine.
please me in solving error without updating null values in database table.
thanks in advance, raj
i recommend "clean" database values, maybe setting column not nullable.
but if can't done, can modify setters, checks null
edit: since double primitive, try wrap double
object
public void setcomm(double comm) { if(null != double.valueof(comm)){ this.comm = comm; }else{ this.comm = 0; } }
hope helps
Comments
Post a Comment