The correct way to use sessionFactory

I am trying to learn the spring 3 and DAO and BO classes and how to auto-install them, and I was wondering if this is the correct way to connect sessionFactory as I read that it is better to use

public void save(Customer customer) {
    sessionFactory.getCurrentSession().save(customer);
}

but not

public void save(Customer customer){
    getHibernateTemplate().save(customer);
}

So, the correct way to connect sessionFactory?

CustomHibernateDaoSupport class

package com.fexco.helloworld.web.util;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class CustomHibernateDaoSupport extends HibernateDaoSupport
{    
@Autowired
@Qualifier("sessionFactory")
public void seSessionFactory(SessionFactory sessionFactory) {

    this.setSessionFactory(sessionFactory);
}
}

Class CustomerDaoImpl

package com.fexco.helloworld.web.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.fexco.helloworld.web.model.Customer;
import com.fexco.helloworld.web.util.CustomHibernateDaoSupport;

@Repository("customerDao")
public class CustomerDaoImpl extends CustomHibernateDaoSupport implements CustomerDao{


@Autowired
private SessionFactory sessionFactory;

public void save(Customer customer) {
    sessionFactory.getCurrentSession().save(customer);
}

Is this correct or am I mistaken because I cannot get it to work? Thanks

+3
source share
1 answer

Here is an explanation of why we do not need templates with Hibernate 3 http://blog.springsource.com/2007/06/26/so-should-you-still-use-springs-hibernatetemplate-andor-jpatemplate/

+1
source

All Articles