2015-04-01 79 views
0

由於數據庫表中的值爲空,我得到「org.hibernate.PropertyAccessException」。如何處理異常?如何在Hibernate應用程序中處理數據庫空值?

我的文件

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(); 
 
\t 
 
\t Session session1=factory.openSession(); 
 
\t Employee emp1=(Employee)session1.get(Employee.class,7839); 
 
\t System.out.println(emp1.getEmpno()+" "+emp1.getEname()+" "+emp1.getSal()); 
 
\t session1.close(); 
 
\t 
 
\t Session session2=factory.openSession(); 
 
\t Employee emp2=(Employee)session2.load(Employee.class,7839); 
 
\t System.out.println(emp2.getEmpno()+" "+emp2.getEname()+" "+emp2.getSal()); 
 
\t session2.close(); 
 
\t 
 
} 
 
}

Employee.java

package com.raj.java.hiberanteDemos; 
 

 
import java.sql.Date; 
 

 

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

 
\t public int getEmpno() { 
 
\t \t return empno; 
 
\t } 
 

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

 
\t public int getMgr() { 
 
\t \t return mgr; 
 
\t } 
 

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

 
\t public int getDeptnumber() { 
 
\t \t return deptnumber; 
 
\t } 
 

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

 
\t public String getJob() { 
 
\t \t return job; 
 
\t } 
 

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

 
\t public double getComm() { 
 
\t \t return comm; 
 
\t } 
 

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

 
\t public Date getHiredate() { 
 
\t \t return hiredate; 
 
\t } 
 

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

 
\t public String getEname() { 
 
\t \t return ename; 
 
\t } 
 

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

 
\t public double getSal() { 
 
\t \t return sal; 
 
\t } 
 

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

 
}

的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> 
 

 
\t <session-factory> 
 
\t \t <property name="hbm2ddl.auto">update</property> 
 
\t \t <property name="dialect">org.hibernate.dialect.Oracle9Dialect 
 
     </property> 
 
\t \t <property name="connection.url">someValidurl</property> 
 
\t \t <property name="connection.username">username</property> 
 
\t \t <property name="connection.password">password</property> 
 
\t \t <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
 
\t \t <property name="hibernate.show_sql">true</property> 
 
\t \t <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property> 
 
\t \t <property name="hibernate.cache.use_second_level_cache">true</property> 
 
\t \t <mapping resource="employee.hbm.xml"></mapping> 
 
\t </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> 
 
\t <!-- <class name="com.javatpoint.mypackage.Employee" table="employee"> <id 
 
\t \t name="empid"> <generator class="assigned"></generator> </id> <property name="firstName"></property> 
 
\t \t <property name="lastName"></property> </class> --> 
 
\t <class name="com.raj.java.hiberanteDemos.Employee" table="emp"> 
 
\t \t <id name="empno" type="int" column="EMPNO"> 
 
\t \t \t <generator class="increment" /> 
 
\t \t </id> 
 
\t \t <property name="ename" type="java.lang.String" /> 
 
\t \t <property name="mgr" type="int" /> 
 
\t \t <property name="deptnumber" type="int" column="deptno" /> 
 
\t \t <property name="job" type="java.lang.String" /> 
 
\t \t <property name="sal" type="double" /> 
 
\t \t <property name="comm" type="double" /> 
 
\t \t <property name="hiredate" type="java.sql.Date" /> 
 

 
\t </class> 
 
</hibernate-mapping>

當我運行該應用程序我得到如下錯誤信息。

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 
 
\t at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:215) 
 
\t at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:185) 
 
\t at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3232) 
 
\t at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129) 
 
\t at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842) 
 
\t at org.hibernate.loader.Loader.doQuery(Loader.java:717) 
 
\t at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) 
 
\t at org.hibernate.loader.Loader.loadEntity(Loader.java:1785) 
 
\t at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:47) 
 
\t at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:41) 
 
\t at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2730) 
 
\t at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365) 
 
\t at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346) 
 
\t at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123) 
 
\t at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:177) 
 
\t at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87) 
 
\t at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862) 
 
\t at org.hibernate.impl.SessionImpl.get(SessionImpl.java:799) 
 
\t at org.hibernate.impl.SessionImpl.get(SessionImpl.java:792) 
 
\t at com.raj.java.hiberanteDemos.FetchTest.main(FetchTest.java:13) 
 
Caused by: net.sf.cglib.beans.BulkBeanException 
 
\t at com.raj.java.hiberanteDemos.Employee$$BulkBeanByCGLIB$$142cfd75.setPropertyValues(<generated>) 
 
\t at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:212) 
 
\t ... 19 more 
 
Caused by: java.lang.NullPointerException 
 
\t ... 21 more

我的表emp數據

EmpData

當我0或其他一些價值它的做工精細更新的所有空值。

請幫助我解決錯誤,而不更新數據庫表中的空值。

由於事先 拉吉

+0

您可以修改setter,以便檢查null值並將其替換爲零。 – jmcg 2015-04-01 05:40:49

+0

@Raj你可以將mgr字段從int更改爲Integer嗎? – vijay 2015-04-01 05:52:08

回答

2

我個人建議實際上是「乾淨」的數據庫值,也許將列設置不能爲空。

但是,如果這個不能做,你能做的就是修改您的制定者,所以它檢查null

編輯: 因爲雙是一種原始的,試圖將其換到一個Double對象

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

    }else{ 
     this.comm = 0; 
    } 
} 

希望這有助於

+0

@ jmcg謝謝你的回答,但我們可以使用!=運算符的雙值爲空值嗎?我不這麼認爲 – Raj 2015-04-01 05:48:52

+0

它是拋出錯誤「運算符!=未定義爲參數類型(s)double,null」 – Raj 2015-04-01 05:51:39

+0

抱歉。我意識到,雙是一個原始的。將你的變量包裝到一個'Double'對象 – jmcg 2015-04-01 05:52:12

0

你的經理,物業是INT,不允許。將其更改爲Integer,它允許null。或者爲您的mgr列設置默認值。

相關問題