我的程序使用 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;
}
部门DAO.java
public interface DepartamentDAO
{
public void afegirDepartament(Departament dep);
public List<Departament> getDepartaments();
}
部门DAOImpl.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());
}
}
部门服务.java
public interface DepartamentService
{
public void afegirDepartament(Departament dep);
public List<Departament> getDepartaments();
}
部门ServiceImpl.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-application-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 为空)。
当我阅读hibernate的输出时,这似乎不是什么奇怪的事情,并且与数据库的连接出现了(hibernate已经正确创建了表app_departaments)。
我不知道我是否在配置中遗漏了什么,或者什么。
我会寻求任何帮助。
堆栈跟踪
java.lang.NullPointerException at com.hpcds.grupgirona.dades.service.imp.DepartamentServiceImp.getDepartaments(DepartamentServiceImp.java:57) at com.hpcds.grupgirona.components.plantilla.LlistatDepartament.encodeEnd(LlistatDepartament.java:51) at 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) 在 com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) 在 org.springframework.faces.webflow.FlowViewHandler.renderView(FlowViewHandler.java:99) 在 com.sun.faces .lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) 在 com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 在 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) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669) at 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) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at 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。调用(AuthenticatorBase.java:472)在 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.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 在 org.apache.tomcat.util 的 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)。 net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 在 java.util.concurrent。ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)