Spring+Hibernate, Autowire sessionFactory into hibernate DAO

雨燕双飞 提交于 2019-11-30 14:43:37

问题


i have an Hibernate DAO, in according with Hibernate API 3 and Spring 3.x, I use simply a sessionFactory and NOT an HibernateDaoSupport+getHibernateTemplate() - i hope this is a good choice... -

Now my goal is to autowire sessionFactory into my DAO using annotations.

In my spring.xml i have this:

<context:component-scan base-package="data" />

Inside data package i have all my DAO and Service classes.

This my simple HibernateDao:

@Repository
public class PersonHDAO implements PersonDAO {

 private SessionFactory sessionFactory;

 @Autowired
 public void setSessionFactory(SessionFactory sessionFactory) {
     this.sessionFactory = sessionFactory;
 }

 public List<Person> findAll(){
  return sessionFactory.getCurrentSession().createQuery("bla bla").list();
 }
}

I have no error during spring.xml loading, but sessionFactory still be null.

What i have to do?

EDIT

This is my sessionFactory declaration in spring.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: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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- <property name="driverClassName" value="org.h2.Driver" /> -->
        <property name="url" value="my/db/url" />
        <property name="username" value="myUsername" />
        <property name="password" value="myPassword" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="data" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>

    <context:component-scan base-package="data" />

    <tx:annotation-driven/>

    <bean id="transactionManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

EDIT2 Now sessionFactory is not null, but i have another kind of exception:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [data.PersonHDAO] is defined: expected single bean but found 0:

Maybe means it can't find PersonHDAO bean?

Thanks all.


回答1:


Did you declare sessionFactory bean?

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/hibernate.cfg.xml</value>
</property>
</bean>



回答2:


I think your problem is @Autowired

@Autowired
private SessionFactory sessionFactory;



回答3:


 @Autowired
 private SessionFactory sessionFactory;

 public void setSessionFactory(SessionFactory sessionFactory) {
     this.sessionFactory = sessionFactory;
 }


来源:https://stackoverflow.com/questions/4128716/springhibernate-autowire-sessionfactory-into-hibernate-dao

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!