我正在尝试将我非常小的应用程序转换为 Spring 3.2 和 Hibernate 4,但我无法让它在会话中正常运行。本段下方的一些信息,但最终 sessionFactory 每次到达 DAO 时总是为空,我不知道为什么。
将控制台与 Hibernate 3 进行比较,Hibernate 3 在启动时显示:
2012 年 11 月 23 日晚上 10:51:52 org.springframework.orm.hibernate3.LocalSessionFactoryBean buildSessionFactory INFO: Building new Hibernate SessionFactory
堆栈跟踪:
java.lang.NullPointerException dao.CSSDAOImpl.selectAllCSS(CSSDAOImpl.java:21)
休眠-cfg.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"></property>
<property name="url" value="jdbc:derby:C:\Users\Steven\MyDB"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan" value="net.form" />
<property name="dataSource" ref="myDataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyTenSevenDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>net.form.StyleChooser</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="CSSDAO" class="dao.CSSDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
道:
package dao;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import net.form.StyleChooser;
public class CSSDAOImpl implements CSSDAO {
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional
public List selectAllCSS() {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
List l = session.createCriteria(StyleChooser.class).list();
session.flush();
tx.commit();
return l;
}
@Transactional
public StyleChooser selectCSSById(Integer ID) {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
StyleChooser sc = (StyleChooser) session.get(StyleChooser.class, ID);
session.flush();
tx.commit();
return sc;
}
@Transactional
public Integer insertCSS(StyleChooser insertCSS) {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Integer id = (Integer) session.save(insertCSS);
session.flush();
tx.commit();
return id;
}
@Transactional
public void deleteCSS(Integer CSSId) {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
StyleChooser sc = (StyleChooser) session.get(StyleChooser.class, CSSId);
session.delete(sc);
session.flush();
tx.commit();
}
@Transactional
public void updateCSS(StyleChooser cssWithNewValues) {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.update(cssWithNewValues);
session.flush();
tx.commit();
}
}
访问 DAO...
package net.controllers;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.form.StyleChooser;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import dao.CSSDAOImpl;
@Controller
@RequestMapping("/stylechoosertable.html")
public class IndexController extends MultiActionController {
Resource resource = new FileSystemResource(
"C:/Users/Steven/Desktop/Programming/workspace/CSSGeneratorHibernate4/WebContent/WEB-INF/hibernate.cfg.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
CSSDAOImpl dao = (CSSDAOImpl) beanFactory.getBean("CSSDAO");
@RequestMapping(method = RequestMethod.GET)
public ModelAndView showIndex(HttpServletRequest request) throws Exception {
List<StyleChooser> styleChooser = dao.selectAllCSS();
return new ModelAndView("stylechoosertable", "styleChooser", styleChooser);
}
}