With a DAO template, do you ever show an EntityManager or Session as a parameter?

Is there ever a case in a standard webapp where you can pass an EntityManager or Session as a parameter to call a DAO, i.e. findPersonByName(String name, Session session)? Or should we abstract the opening and closing of the session during implementation?

+3
source share
2 answers

A better approach would be to initialize or otherwise introduce DAO with SessionFactory. Then you can do things like this:

public abstract class AbstractHibernateDao<T extends Object>
    implements AbstractDao<T> {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    public void save(T t) { getSession().save(t); }

    public void update(T t) { getSession().update(t); }

    ...
}

without going Sessionall over the place.

+7
source

, , . , ( DAO), (/), / .

0

Source: https://habr.com/ru/post/1704007/


All Articles