I'm starting to study Hibernate
and wanted to "start right".
The doubt is as follows:
For the entire transaction I need to use the following code:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
and to save
Empregado emp = new Empregado();
emp.setEmail("[email protected]");
emp.setNome("Jose");
emp.setSobenome("Alves");
session.save(emp);
session.getTransaction().commit();
I am already accustomed to the DAO
pattern but using JDBC
.
I'd like to know how best to use Sessions
along with the DAO
pattern.
If, for example, I would have to open Session
and close in all DAO
methods, or if I would have some other way not to allocate unnecessary resources.
If you can paste an example I would appreciate it.