How to adapt Hibernate to the DAO standard?

Asked

Viewed 376 times

3

I’m starting to study Hibernate and wanted to "start the right way". The doubt is as follows:

For any 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’m used to the pattern DAO but using JDBC. I would like to know how best to use Sessions along with the pattern DAO. If, for example, I had to open the Session and close in all methods of DAO, or if there was some other way not to allocate unnecessary resources. If you could paste an example I’d appreciate it.

  • What is the question exactly? For a model class, you create a dao correspondent? If so, I see no problem with that. For example: suppose you have an employee.java class within the model, with its characteristics. On the dao, suppose you have Empregadodao.java with the method save(). In this method, you will put this code that you showed in the question. Every time you need to save an employee, just call the method save() for that employee. That’s what you do?

  • It would be more or less that. My real doubt is whether in all the methods of DAOI would have to keep opening the Session and the Transaction repeatedly or if there would be any better way to do it.

  • Got it. I use it this way, having seen some code on the net. But I don’t know if it’s the best way.

  • You always have to open and close Session, and you always have to start and close the transaction. But it doesn’t make much sense to do this in each DAO method because you may want to use more than one method in the same transaction. Generally, you get Session and initialize the transaction at the beginning of each application service, and you can use frameworks to save service. The best way to do it, however, depends on more information about your system.

1 answer

2


The DAO Standard is nothing more than isolating all database access in a single layer. In java we use packages to isolate access.

JDBC is nothing more than the API provided by JAVA that allows you access to databases, but this is dependent on the implementation you will use, in case the drive you will add to your project.

update

Here is a very basic example of a layered Model, DAO and Service system. It is possible to see that each class performs only one activity. For a more complete example is missing only the VIEW layer of information presentation.

public class Pessoa{

    private Int id;
    private String nome;
    private Int idade;
    //...outro metodos
}

public class PessoaDao{

    public Boolean salvar(Pessoa pessoa){

        Boolean resultado = false;
        try{
            Connection con = Factory.getConnection();
            String insertComand = "insert into tb_pessoa values(?,?,?)";
            PreparedStatement cmd=con.preparedStatement(insertComand);
            cmd.setInt(1,pessoa.getId());
            cmd.setString(2,pessoa.getNome());
            cmd.setInt(3,pessoa.getIdade());
            cmd.execute();
            resultado=true;
        }catch(Exception ex){
            resultado=false;
        }

        return resultado;
    }

}

public class PessoaService{

    private PessoaDao pessoaDao;

    public boolean salvarPessoa(Pessoa pessoa){
        pessoaDao = new PessoaDao();
        return pessoaDao.salvar(pessoa);
    }
}
  • 3

    The answer is good, but it would be better if you had an example that doesn’t depend on an external link. :)

  • 1

    I also agree with what @utluiz said. Also, the link is no longer working (404).

  • 1

    JDBC is not the drive itself, it is an API that provides forms of interaction with the database. It does not work if there is no DBMS driver added.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.