복붙노트

[SPRING] Spring + Hibernate, 최대 절전 모드 DAO로 sessionFactory 자동로드

SPRING

Spring + Hibernate, 최대 절전 모드 DAO로 sessionFactory 자동로드

나는 Hibernate DAO를 가지고있다. Hibernate API 3과 Spring 3.x에 따르면, 나는 단순히 sessionFactory를 사용하고 HibernateDaoSupport + getHibernateTemplate ()는 사용하지 않는다. - 이것이 좋은 선택이되기를 희망한다 ... -

이제는 내 목표는 Annotation을 사용하여 DAO에 sessionFactory를 autowire하는 것입니다.

내 spring.xml에서 나는 이것을 가지고있다 :

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

데이터 패키지 안에는 모든 DAO 및 Service 클래스가 있습니다.

이 내 간단한 최대 절전 모드 Dao :

@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입니다.

내가 뭘해야 해?

편집하다

spring.xml의 sessionFactory 선언입니다.

<?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 이제 sessionFactory는 null이 아니지만 다른 종류의 예외가 있습니다.

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

어쩌면 PersonHDAO 빈을 찾을 수 없다는 뜻인가?

모두에게 감사드립니다.

해결법

  1. ==============================

    1.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>
    
  2. ==============================

    2.나는 너의 문제가 @Autowired라고 생각한다.

    나는 너의 문제가 @Autowired라고 생각한다.

  3. ==============================

    3.

     @Autowired
     private SessionFactory sessionFactory;
    
     public void setSessionFactory(SessionFactory sessionFactory) {
         this.sessionFactory = sessionFactory;
     }
    
  4. from https://stackoverflow.com/questions/4128716/springhibernate-autowire-sessionfactory-into-hibernate-dao by cc-by-sa and MIT license