一尘不染

如何在Hibernate Application中处理数据库空值?

hibernate

由于数据库表中的值为空,我得到“ org.hibernate.PropertyAccessException” 。如何处理异常?

我的档案是

FetchTest.java

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

我的emp表数据

EmpData

当我用0或其他一些值更新所有null值时,它的工作
正常。

请帮助我解决错误而不更新数据库
表中的空值。

Thanks in Advance


阅读 257

收藏
2020-06-20

共1个答案

一尘不染

我个人建议实际“清理”数据库值,也许将列设置为不可为空。

但是,如果不能这样做,则可以修改设置器,以便检查 null:

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

    }else{
        this.comm = 0;
    }
}

希望这可以帮助

2020-06-20