14

Hibernate API 3およびSpring 3.xに従って、Hibernate DAOを使用しています。単純に + ではなく a を使用します-これが良い選択であることを願っています... -sessionFactoryHibernateDaoSupportgetHibernateTemplate()

今の私の目標は、sessionFactory注釈を使用して DAO に自動配線することです。

spring.xmlはこれを持っています:

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

データ パッケージ内には、すべての DAO クラスと Service クラスがあります。

これは私の簡単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();
 }
}

spring.xmlロード中にエラーはありませんが、sessionFactoryそれでもnull.

私がしなければならないこと?

編集

これは私の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>

EDIT2sessionFactoryはnullではありませんが、別の種類の例外があります :

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

PersonHDAO豆が見つからないということでしょうか?

皆さんありがとう。

4

4 に答える 4

14

sessionFactory Bean を宣言しましたか?

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/hibernate.cfg.xml</value>
</property>
</bean>
于 2010-11-08T22:30:46.393 に答える
3

あなたの問題は@Autowiredだと思います

@Autowired
private SessionFactory sessionFactory;
于 2014-06-20T13:08:30.673 に答える
1
 @Autowired
 private SessionFactory sessionFactory;

 public void setSessionFactory(SessionFactory sessionFactory) {
     this.sessionFactory = sessionFactory;
 }
于 2014-12-04T04:44:58.277 に答える