我正在尝试集成Spring + Hibernate4,但是当我访问我的DAO时,我在sessionFactory对象上得到了一个nullPointerException。
这是我的AbstractDao:
@Repository
public class AbstractDaoHibernateImpl {
private SessionFactory sessionFactory;
public AbstractDaoHibernateImpl() {
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
protected Session getSession() {
return getSessionFactory().getCurrentSession();
}
protected Order getOrder(String orderBy, boolean isOrderAsc) {
if (isOrderAsc)
return Order.asc(orderBy);
else
return Order.desc(orderBy);
}
}
这是我的实体bean配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- transacciones -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory"/>
</bean>
<!--Beans de los servicios de la aplicacion -->
<bean id="userService" class="com.app.service.user.UserServiceImpl"/>
<bean name="userDAO" class="com.app.model.user.dao.UserDaoImpl">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
这是我的appContext的其余部分
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:component-scan base-package="com.app.controllers" />
<mvc:annotation-driven />
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager"/>
<import resource="applicationDatasource.xml" />
<import resource="applicationEntityBean.xml" />
</beans>
最后这是我的服务对象:
@Service
public class UsuarioServiceImpl implements IUsuarioService {
@Autowired
private IUserDao userDao;
public List<User> getUsers() throws GenericDataBaseException {
return userDao.findAll();
}
}
不应该像这样注入sessionFactory对象吗?
这是整个堆栈跟踪:
java.lang.NullPointerException
at es.plexus.core.dao.impl.GenericDaoHibernateImpl.findAll(GenericDaoHibernateImpl.java:243)
at es.plexus.service.usuario.UsuarioServiceImpl.getUsuarios(UsuarioServiceImpl.java:20)
at es.plexus.controller.usuario.UsuarioController.listUsuarios(UsuarioController.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at 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:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:722)
编辑:userDao:
public class UserDaoImpl extends GenericDaoHibernateImpl<Usuario, Long> implements IUserDao {
}
扩展了GenericDao:
public class GenericDaoHibernateImpl<E, PK extends Serializable> extends AbstractDaoHibernateImpl implements GenericDao<E, PK> {
private Class<E> entityClass;
public GenericDaoHibernateImpl() {
this.entityClass = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public List<E> findAll() throws GenericDataBaseException {
try {
Criteria criteria = getSession().createCriteria(getEntityClass());
List<E> result = criteria.list();
/*
* if (result == null || result.size() == 0) throw new
* NoSearchResultException("", getEntityClass().getName());
*/
return result;
} catch (Throwable t) {
Collection<Object> args = new ArrayList<Object>();
throw exceptionHandler.handle(this, t, "findAll", args);
}
}
(...)
}
答案 0 :(得分:1)
我看不到代码中任何地方都没有定义任何交易。
在这种情况下,我建议您将它们放在您的服务方法上:
@Transactional(readOnly = true)
public List<User> getUsers() throws GenericDataBaseException {
return userDao.findAll();
}
但您似乎正在开发一个相当复杂的类层次结构,您只需要一个通用存储库,然后instantiate that for different types。您不必使用服务类,仅在使用多个聚合根/复杂逻辑时推荐它们 - 而不是简单的getter。
答案 1 :(得分:0)
让我猜一下。
您的DAO是否在com.app.controllers
包裹下?如果没有像dao
<context:component-scan base-package="com.app.controllers, com.app.model.user.dao" />
包添加到组件扫描中