Announcement Announcement Module
No announcement yet.
SessionFactory / HibernateTemplate & Annotation Configuration Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • SessionFactory / HibernateTemplate & Annotation Configuration

    Hi Guys,

    I'm sure this is a popular question, but I've been trawling the net for the last few hours without finding a workable solution.

    I'm trying to go completely with annotation configuration, and the problem I'm having is with the dreaded 'sessionFactory' or 'hibernateTemplate' is required

    So my DAO classes look something like:

    public class SampleDaoImpl extends HibernateDaoSupport implements SampleDao {
        public void deleteSample( Sample sample ) {
            getHibernateTemplate().delete( sample );
        // etc

    The question I have is, how do I get the SessionFactory or HibernateTemplate to be automatically injected into this DAO object?

    Thanks in advance!

  • #2
    Re: SessionFactory / HibernateTemplate & Annotation Configuration


    these days I am involved in project that is build upon Hibernate annotation, Spring 2.5.6 (using annotation) so I propose next.

    I still have ApplicationContext (dataApplicationContext.xml) where I defined two main beans:
    1.) dataSource (based on BasicDataSource)
    2.) sessionFactory (based on hibernate 3 annotation session factory)

    These two beans can be accessed in both manner: through getting bean from aplicationContext or using annotation @Autowired.

    In my DAO implementation I have next situation:

    public class CountryDAOImpl 
    	extends HibernateDaoSupport implements CountryDAO {
    	public CountryDAOImpl(SessionFactory sessionFactory) {
            ... /* next code is omitted */
    So as You can see from code @Autowired will try to find best implementation by type (in this case SessionFactory) and will wire sessionFactory in DAOImpl.

    I think explanation is ok, but if You still have question about this You can ask latter.

    NOTE: in applicationContext You have to set both:

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