0

我对使用 JSF、Spring、Hibernate 和 Maven 非常陌生。但到目前为止,我创建了一个简单的项目,它在 jsf 页面上接受名字和姓氏。将其传递给托管 bean > 然后将其传递给我的服务层的单独模块上的 pojo。这一切都有效,但现在我正在尝试在项目中实现休眠。我在服务层的 pom 中添加了几个依赖项,在资源文件夹(db.properties、applicationContext.xml、datasource.xml、HibernateSessionFactory.xml)中创建了一些文件,并创建了一个快速的小 dao 文件。架构很糟糕,但在我开始设计一个大项目之前,我只想获得一个快速示例。

我的 Web 层上的应用程序上下文文件只是将我的托管 bean 设置为连接到我的服务层。它没有任何休眠配置,因为我想在我的服务层上进行所有处理和数据库交互。另外,我在我的 pojo 上使用休眠注释。

当我运行我的服务器时,我可以访问我的网页,输入名字和姓氏,然后点击提交。当我调试时,它将变量从我的托管 bean 设置到我的服务层,然后我的服务层从 customerdao 对象调用 addCustomer。我的 getHibernateTemplate().save(customer); 上出现空指针异常;线。

WARNING: #{formBean.findBalance}: java.lang.NullPointerException
javax.faces.FacesException: #{formBean.findBalance}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:117)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
    ... 23 more
Caused by: java.lang.NullPointerException
    at com.scott.common.test.Customer.save(Customer.java:52)
    at com.scott.common.CustomerBackingBean.findBalance(CustomerBackingBean.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 24 more

这是我的 HibernateSessionFactory.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- Hibernate session factory -->
<bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>

    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
         <prop key="hibernate.show_sql">true</prop>
       </props>
    </property>

    <property name="annotatedClasses">
    <list>
        <value>com.scott.common.test.Customer</value>
    </list>
    </property> 

</bean>
</beans>

这是我的 db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/scottdb
jdbc.username=djdjdi
jdbc.password=dldljdl

This is my datasource.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean 
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location">
        <value>db.properties</value>
   </property>
</bean>

  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
  </bean>

</beans>

这是我的 applicationContext.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

        <bean id="customerDao" 
             class="com.scott.common.test.CustomerDAO" >
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>

</beans>

这是我的customerdao类

package com.scott.common.test;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


public class CustomerDAO extends HibernateDaoSupport
{   
    public void addCustomer(Customer customer)
    {
        customer.setDescript("Test description");
        getHibernateTemplate().save(customer);
    }
}
4

2 回答 2

0

只是一个猜测 -customer可能null。尝试进行此更改,以检查客户对象是否为空:

public class CustomerDAO extends HibernateDaoSupport
{   
    public void addCustomer(Customer customer)
    {
        if (customer == null ) {
            return; // do nothing if the customer is null
        }
        customer.setDescript("Test description");
        getHibernateTemplate().save(customer);
    }
}

任何时候你在一个为空的对象上调用一个方法,你都会得到一个 NPE。

另一种选择是抛出一个更有帮助的异常:

if (customer == null ) {
     throw new IllegalArgumentException("customer may not be null");
}
于 2012-12-14T23:41:00.343 回答
0

只是另一个猜测 - 可能 Spring bean 没有正确初始化。尝试检查:

  1. customerDAO 是通过 Spring 还是某个明确的 new CustomerDAO() 创建的?
  2. 检查调试器 sessionFactory 和其他与会话相关的属性。

更新:查看代码后,猜测得到证实:Spring 配置错误。在 2 个 eclipse 项目中有 2 个独立的应用程序上下文。需要将其合并为一个(用于 Web 应用程序)。

于 2012-12-14T23:58:55.070 回答