Hibernate ServiceImplementation抛出NullPointerException

时间:2013-01-16 12:17:50

标签: java spring hibernate nullpointerexception


使用Spring和Hibernate我的程序有问题 我有这个课程:
departament.java

@Entity
@Table(name="apps_departaments")
public class Departament
{
    @Id
    @GeneratedValue
    private int id;

    @Column(name="dep_codi")
    private String CodDep;

    @Column(name="dep_nomc")
    private String nom;

    @Column(name="dep_nomm")
    private String nomCurt;
}

departamentDAO.java

public interface DepartamentDAO
{
    public void afegirDepartament(Departament dep);
    public List<Departament> getDepartaments();
}

departamentDAOImpl.java

@Repository
public class DepartamentDAOImpl implements DepartamentDAO
{
    @Autowired
    private SessionFactory sessionFactory;

    public void afegirDepartament(Departament dep)
    {
        sessionFactory.getCurrentSession().save(dep);
    }

    public List<Departament> getDepartaments()
    {
        return (List<Departament>)(sessionFactory.getCurrentSession().createCriteria(Departament.class).list());
    }
}

departamentService.java

public interface DepartamentService
{
    public void afegirDepartament(Departament dep);
    public List<Departament> getDepartaments();
}

departamentServiceImpl.java

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class DepartamentServiceImp implements DepartamentService
{
    @Autowired
    private DepartamentDAO depDAO;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    @Override
    public void afegirDepartament(Departament dep)
    {
        depDAO.afegirDepartament(dep);
    }

    @Override
    public List<Departament> getDepartaments() {
        return depDAO.getDepartaments();
    }
}

我的配置设置为:
web的应用程序-config.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"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="net.grupgirona.intranet" />
    <context:annotation-config />
    <context:property-placeholder location="classpath*:jdbc.properties"/>
    <import resource="webmvc-config.xml" />
    <import resource="webflow-config.xml" />
    <import resource="data-access-config.xml" />
</beans>

数据存取config.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"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
    p:password="${jdbc.password}" />

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="packagesToScan" value="net.grupgirona.dades"/>
  </bean>

  <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
  </bean>
</beans>

好的,当我尝试以这种方式访问​​数据时:

DepartamentServiceImp dades = new DepartamentServiceImp();

我得到一个NullPointerException(调试时dades为null) 当我读取hibernate输出时,看起来并不奇怪,并且与数据库的连接出现了(hibernate已经正确地创建了表app_departaments)。
我不知道我是否遗漏了配置中的东西,或者是什么
我会提供任何帮助。

栈跟踪

显示java.lang.NullPointerException     在com.hpcds.grupgirona.dades.service.imp.DepartamentServiceImp.getDepartaments(DepartamentServiceImp.java:57)     在com.hpcds.grupgirona.components.plantilla.LlistatDepartament.encodeEnd(LlistatDepartament.java:51)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1854)     在javax.faces.component.UIComponent.encodeAll(UIComponent.java:1850)     在com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:440)     at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)     在org.springframework.faces.webflow.FlowViewHandler.renderView(FlowViewHandler.java:99)     at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)     在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)     at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:202)     在org.springframework.faces.mvc.JsfView.renderMergedOutputModel(JsfView.java:85)     在org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)     在org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)     在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)     在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)     在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)     在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:621)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)     在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)     在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)     在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)     在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)     在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)     在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)     在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)     在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)     在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)     在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:310)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在java.lang.Thread.run(未知来源)

1 个答案:

答案 0 :(得分:1)

你不能使用这一行

DepartamentServiceImp dades = new DepartamentServiceImp();

因为你在DepartamentServiceImp中有一个@Autowired注释状态变量。

Spring不鼓励使用new关键字。您需要的所有实例都必须从spring上下文中实例化和检索。

DepartamentDAO中设置一个setter DepartamentServiceImp,如果您仍想自己实例化该对象,请设置dao。