Question

I run in few huge problems by using getSession() on HibernateDaoSupport and now when i try to fix it I was wondering if it is right to make a abstract class like this bellow and make all Dao's to extend it instead of adding SessionFactory in each Dao ?

If it is, then would creating bean of this abstract Dao class and passing it the session factory then work once other Dao's extend it? Or that is not even possible?

public abstract class AbstractDAOImpl<T> implements
        AbstractDAO<T> {

    private static Logger   _logger = LoggerFactory
                                    .getLogger(AbstractDAOImpl.class);
    private SessionFactory  factory;

    @Override
    public void refresh(final T object) {
        try {
            factory.getCurrentSession().refresh(object);
        } catch (Exception e) {
            _logger.error("Cannot refresh object " + object, e);
        }
    }

    @Override
    public void remove(final T object) {
        try {
            factory.getCurrentSession().delete(object);
        } catch (Exception e) {
            _logger.error("Cannot remove object " + object, e);
        }
    }

    @Override
    public void save(final T object) {
        try {
            factory.getCurrentSession().saveOrUpdate(object);
        } catch (Exception e) {
            _logger.error("Cannot save or update object " + object, e);
        }
    }

}
Was it helpful?

Solution

public interface RootDAO<T> extends Serializable {

    public List<T> loadAll();

    public T save(T entity);

    public void delete(T entity);

    public void markAsDeleted(T entity);

    public T get(Serializable id);

    public T load(Serializable id);

    public void saveOrUpdate(T entity);

    public void deleteAll(Collection<T> entities);

    public void saveOrUpdateAll(Collection<T> entities);

    public List<T> find(String hql);

    public void update(T entity);

    public T getByExampleUnique(T entity);

    public List<T> getByExampleList(T entity);

    public List<T> listAll();

    public Object execute(HibernateCallback action);

    public List<T> findByNamedParam(String queryString, String paramName,Object value);

    public List<T> findByNamedParam(String queryString, String[] paramNames,Object[] values);
    .
    .
    .
    .

}

@Component
public abstract class RootDAOImpl<T> extends HibernateDaoSupport implements RootDAO<T> {


    protected Logger logger = LoggerFactory.getLogger(getClass());
    private Class<T> clazz;

    @Autowired
    public void init(SessionFactory factory) {
        setSessionFactory(factory);
    }

    public RootDAOImpl(Class<T> clazz) {
        this.clazz = clazz;
    }

    public void delete(T entity) {
        getHibernateTemplate().delete(entity);
    }

    public void delete(String id) {
        getHibernateTemplate().delete(new FbUser(id));
    }

    public void markAsDeleted(T entity) {
        // Mark entity as deleted
        try {
            Method setDeletedMethod = clazz.getDeclaredMethod("setDeleted", Boolean.class);
            setDeletedMethod.invoke(entity, true);
            getHibernateTemplate().saveOrUpdate(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // actually delete
        // getHibernateTemplate().delete(entity);
    }

    @Override
    public void deleteAll(Collection<T> entities) {
        getHibernateTemplate().deleteAll(entities);
    }

    @Override
    public void saveOrUpdateAll(Collection<T> entities) {
        getHibernateTemplate().saveOrUpdateAll(entities);
    }

    @SuppressWarnings("unchecked")
    @Override
    public T get(Serializable id) {
        return (T) getHibernateTemplate().get(clazz, id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public T load(Serializable id) {
        return (T) getHibernateTemplate().load(clazz, id);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<T> find(String hql) {
        return (List<T>) getHibernateTemplate().find(hql);
    }

    @Override
    public Object execute(HibernateCallback action) {
        return getHibernateTemplate().execute(action);
    }


    .
    .
    .

}

@Repository
public class UserDAOImpl extends RootDAOImpl<User> implements UserDAO{


    public UserDAOImpl() {
        super(User.class);
    }
}

If you are not using a DI framework you may need to keep a reference for SessionFactory and pass it yourself when you create the DAO instance.

OTHER TIPS

This is exactly why people use JPA implementation by hibernate. You just need to start using the JPA's EntityManager which leverages on SessionFactory by itself in the best possible design patterns. You dont have to reinvent the whole design patterns here. All you need to do is just use CRUD operations of EntityManager in each of your DAO as shown in the following example. All the best with your implementation.

http://www.myhomepageindia.com/index.php/2009/04/02/jpa-hibernate-with-oracle-on-eclipse.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top