Spring 3,带有通用 DAO 的 Hibernate 4 AutoWired sessionFactory
Posted
技术标签:
【中文标题】Spring 3,带有通用 DAO 的 Hibernate 4 AutoWired sessionFactory【英文标题】:Spring 3, Hibernate 4 AutoWired sessionFactory with Generic DAO 【发布时间】:2013-06-07 15:30:00 【问题描述】:使用 Spring MVC 3 和 Hibernate 4 我得到:
java.lang.NullPointerException
com.daniel.rr.dao.common.DAOImpl.getSession(DAOImpl.java:22)
com.daniel.rr.dao.common.DAOImpl.save(DAOImpl.java:27)
com.daniel.rr.controller.HelloController.printWelcome(HelloController.java:30)
这是相关的spring配置:
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>com.daniel.rr.model</value>
<value>com.daniel.rr.dao</value>
<value>com.daniel.rr.dao.common</value>
<value>com.daniel.rr.controller</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.mysql5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/rr" />
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
通用 DAO:
public interface DAO<T>
T save(T t);
void delete(T t);
@Transactional
public abstract class DAOImpl<T> implements DAO<T>
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory)
this.sessionFactory = sessionFactory;
public Session getSession()
return sessionFactory.getCurrentSession();
@Override
public T save(T t)
getSession().persist(t);
return t;
@Override
public void delete(T t)
getSession().delete(t);
AccountDAO:
public class AccountDAO extends DAOImpl<Account> implements DAO<Account>
MVC 控制器:
public class HelloController
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model)
//System.out.println("is null? " + sessionFactory);
AccountDAO dao = new AccountDAO();
Account account = new Account();
account.setUsername("hello");
account.setPassword("test");
dao.save(account);
model.addAttribute("message", "Hello world!");
return "hello";
我已经通过 *** 和 Google 进行了搜索,但找不到任何有用的信息。 我尝试在 DAOImpl.save(T t) 上使用事务,但仍然存在同样的问题。
public T save(T t)
Transaction tr = getSession().getTransaction();
tr.begin();
getSession().persist(t);
tr.commit();
return t;
【问题讨论】:
【参考方案1】:注解不会被继承。
将事务注释添加到具体 dao,而不是抽象类。
You might like this too
【讨论】:
谢谢!找到了解决办法。以上是关于Spring 3,带有通用 DAO 的 Hibernate 4 AutoWired sessionFactory的主要内容,如果未能解决你的问题,请参考以下文章