Spring+Hibernate,Autowire sessionFactory 到 Hibernate DAO

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4128716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:11:39  来源:igfitidea点击:

Spring+Hibernate, Autowire sessionFactory into hibernate DAO

hibernatespringautowiredsessionfactory

提问by blow

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

我有一个Hibernate的DAO,在根据Hibernate的API 3春天3.x中,我用一个简单的sessionFactoryNOTHibernateDaoSupport+ getHibernateTemplate()-我希望这是一个不错的选择-

Now my goal is to autowire sessionFactoryinto my DAO using annotations.

现在我的目标是sessionFactory使用注释自动连接到我的 DAO。

In my spring.xmli have this:

在我的spring.xml我有这个:

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

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

在数据包中,我有我所有的 DAO 和 Service 类。

This my simple HibernateDao:

这是我的简单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.xmlloading, but sessionFactorystill be null.

我在spring.xml加载过程中没有错误,但sessionFactory仍然是null.

What i have to do?

我必须做什么?

EDIT

编辑

This is my sessionFactorydeclaration in spring.xml:

这是我的sessionFactory声明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>

EDIT2Now sessionFactoryis not null, but i have another kind of exception:

EDIT2现在sessionFactory不为空,但我有另一种例外:

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 PersonHDAObean?

也许意味着它找不到PersonHDAObean?

Thanks all.

谢谢大家。

采纳答案by Maurizio Cucchiara

Did you declare sessionFactory bean?

您是否声明了 sessionFactory bean?

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

回答by ibrahimKiraz

I think your problem is @Autowired

我认为你的问题是@Autowired

@Autowired
private SessionFactory sessionFactory;
@Autowired
private SessionFactory sessionFactory;

回答by iCrazybest

 @Autowired
 private SessionFactory sessionFactory;

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