Galera, eu e meus companheiros de trabalho discutimos e criamos um modelinho de DAO Generic orientado a composição e acoplado com o Hibernate.
Gostaríamos muito da ajuda de vocês pois em nossa opinião este modelo pode melhorar e muito porém, o nosso conhecimento ainda não nos permite tal melhora, gostaríamos de opiniões, criticas e ideias…
Agradecemos desde já e aguardamos respostas…
Código da Interface DAO
package dao;
import java.io.Serializable;
import java.util.List;
public interface DAO <T>{
public void save(T t);
public void update(T t);
public void delete(T t);
public void delete(Serializable id);
public T findByID(Serializable id);
public List<T> findAll();
}
Implementação da interface genérica
package dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class GenericDAO<T> implements DAO<T> {
private Session session;
public GenericDAO(Session session) {
this.session = session;
}
@Override
public void save(T t) {
Transaction transaction = null;
try {
transaction = this.session.beginTransaction();
this.session.save(t);
transaction.commit();
} catch (Exception e) {
throw new HibernateException(e);
}
}
@Override
public void update(T t) {
}
@Override
public void delete(T t) {
// TODO Auto-generated method stub
}
@Override
public void delete(Serializable id) {
// TODO Auto-generated method stub
}
@Override
public T findByID(Serializable id) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<T> findAll() {
// TODO Auto-generated method stub
return null;
}
}
Entidade de exemplificação
package entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Teste_Pessoa")
public class Pessoa implements Serializable {
private static final long serialVersionUID = 4687012010792654501L;
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
@Column(name = "nome")
private String nome;
public Integer getId() {
return id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((nome == null) ? 0 : nome.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pessoa other = (Pessoa) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (nome == null) {
if (other.nome != null)
return false;
} else if (!nome.equals(other.nome))
return false;
return true;
}
}
Interface referente ao exemplo
package dao;
import entity.Pessoa;
public interface IPessoaDAO {
public void save(Pessoa pessoa);
}
Implementação desta interface de exemplo
package dao;
import entity.Pessoa;
public class PessoaDAO implements IPessoaDAO {
private DAO<Pessoa> dao;
public PessoaDAO(GenericDAO<Pessoa> dao){
this.dao = dao;
}
@Override
public void save(Pessoa t) {
dao.save(t);
}
}
Util’s
package util;
import dao.GenericDAO;
import dao.IPessoaDAO;
import dao.PessoaDAO;
import entity.Pessoa;
public class HibernateFactory {
public static IPessoaDAO getPessoaDAO(){
PessoaDAO pessoaDAO = new PessoaDAO(new GenericDAO<Pessoa>(HibernateUtil.getSessionFactory().openSession()));
return pessoaDAO;
}
}
package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory SESSION_FACTORY = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration();
annotationConfiguration.configure("hibernate.cfg.xml");
return annotationConfiguration.buildSessionFactory();
} catch (Throwable e) {
e.printStackTrace();
throw new ExceptionInInitializerError();
}
}
public static SessionFactory getSessionFactory(){
return SESSION_FACTORY;
}
}
Mero teste:
package test;
import util.HibernateFactory;
import dao.IPessoaDAO;
import entity.Pessoa;
public class TestePessoa {
public static void main(String[] args) {
Pessoa pessoa = new Pessoa();
pessoa.setNome("Renan Mais teimoso que o CASSAU");
IPessoaDAO dao = HibernateFactory.getPessoaDAO();
dao.save(pessoa);
System.out.println(pessoa);
}
}
Novamente obrigado…