12

I am getting an exception saying :

java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required

When trying to use the @Repository annotation on a HibernateDaoSupport class. The error message is straightforward, in order to create the Repository it needs a sessionFactory. However,I have defined a session factory in my XML:

<!-- Hibernate -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dashDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.mycomp.myapp.Category</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            </props>
        </property>
    </bean>

So I'm not sure how to give the repository the SessionFactory that it requires while it's creating it's annotation driven beans, I attempted to do the following:

 @Autowired
    protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {
        return super.createHibernateTemplate(sessionFactory);
    }

But this does not solve the problem, likely because the repository needs that property while instantiating, not just when performing an action. Unfortunately, I don't know how to get around this problem because there are no constructors or initialization methods to override with a @Autowired annotation.

I checked to make sure the sessionFactory bean is being created and can be Autowired, and that is fine.

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
walnutmon
  • 5,873
  • 7
  • 42
  • 57
  • 3
    You may not want to use Hibernatetemplate anymore as it does not offer much benefit http://blog.springsource.org/2007/06/26/so-should-you-still-use-springs-hibernatetemplate-andor-jpatemplate/ – vsingh Nov 17 '11 at 18:16

2 Answers2

36

HibernateDaoSupport is supplied with SessionFactory via setSessionFactory(). However, setSessionFactory() is final, so you can't override it to add an @Autowired annotation. But you can apply @Autowired to the arbitrary method and call setSessionFactory() from it:

@Autowired
public void init(SessionFactory factory) {
    setSessionFactory(factory);
}
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 1
    awesome, thanks! I already started to go down the route of using the session factory directly, but was struggling to open transactions, I think I will just go with the DAOSupport though, as it works great for my simple purposes. – walnutmon Jul 12 '10 at 18:05
6

You can also define which session factory you want to use (if you have more than one, for instance), by using the @Qualifier annotation:

@Autowired
public MyClassImpl(@Qualifier("myOtherSessionFactory") SessionFactory sessionFactory) {
    setSessionFactory(sessionFactory);
}

You can also toggle between data sources by extending the AbstractRoutingDataSource. See this page.

atrain
  • 9,139
  • 1
  • 36
  • 40
  • atrain@ If we separate data sources, do we need to create separate sessionFactories ? if yes, Can you please give an example how to use (inject) two different session Factories in the DAO implementation ( which implements HibernateDaoSupport ) – learner Oct 25 '21 at 01:37