最近打算整合最新的spring3和hibernate3.6。但是老是出现问题。
首先我引入了所需要的jar包。
我的applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
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
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"> <context:annotation-config />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/home?characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="11111" />
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.bfa.model"/>
</bean>
<bean id="recordDao" class="com.bfa.daoImp.RecordDaoImp"/>
<bean id="recordService" class="com.bfa.serviceImp.RecordServer">
<property name="recordDao" ref="recordDao"></property>
</bean>

</beans>
我在hibernate3.6 中使用了annotation。所以我想在spring中也使用annotation。我初步的设想是将sessionFactory注入到recordDao中。但是,当我测试的时候却报了一个空指针错误。recordDaoImp 中的内容如下:private SessionFactory sessionFactory; public SessionFactory getSessionFactory() {
return sessionFactory;
} @Resource
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List<Record> getAll() throws SQLException {
List<Record> list=new ArrayList<Record>();
System.out.println(this.getSessionFactory());//这里打印出来一个null
Session session=this.sessionFactory.getCurrentSession();
session.beginTransaction();
list=session.createQuery("from Record order by id desc").list();
session.getTransaction().commit();
return list;
}请大家帮我看看是什么原因。

解决方案 »

  1.   

    sessionFactory配置不全,所以注入失败。
    加上:
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop>
    <prop key="hibernate.show_sql">false</prop>
    <prop key="hibernate.format_sql">false</prop>
    <prop key="hibernate.jdbc.batch_size">50</prop>
    <prop key="hibernate.query.substitutions">true 1, false 0</prop>
    <prop key="hibernate.connection.SetBigStringTryClob">true</prop>
    </props>
    </property>
      

  2.   

    首先对你的回复表示感谢,但是我按照你的提示之后测试还是报空指针错误。也就是刚才打印出来的还是一个null。
      

  3.   

    没有注入进去!首先你的dao实现类要继承hibernate提供的HibernateDaoSupport
    然后写方法注入
    例如:
    @Resource(name="sessionFactory")
    public void setSuperSessionFactory(SessionFactory sessionFactory){
         super.setSessionFactory(sessionFactory);
    }
      

  4.   

    修改上面的
    没有注入进去!首先你的dao实现类要继承Spring提供的HibernateDaoSupport
    然后写方法注入
    例如:
    @Resource(name="sessionFactory")
    public void setSuperSessionFactory(SessionFactory sessionFactory){
      super.setSessionFactory(sessionFactory);
    }
      

  5.   

    楼主是自己写的SessionFactory,Spring没有办法去管理。