Olá pessoal,
estou com um problema no Hibernate,ele não está conseguindo abrir a sessão(eu acho que é isso)!!!
estou postando os codigos das minhas classes e do arquivo de configuração do hibernate!
fico no aguardo…obrigado!!=D
Cliente
package br.com.services.modelo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Cliente {
@Id
@GeneratedValue
private long id;
private String nome;
private String rg;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getRg() {
return rg;
}
public void setRg(String rg) {
this.rg = rg;
}
}
ClienteLogic
package br.com.services.logic;
import br.com.services.dao.DaoFactory;
import br.com.services.modelo.Cliente;
public class ClienteLogic {
private final DaoFactory daoFactory;
public ClienteLogic(DaoFactory daoFactory) {
this.daoFactory = daoFactory;
}
public void adiciona(Cliente cliente) {
this.daoFactory.beginTransaction();
this.daoFactory.getClienteDao().adiciona(cliente);
this.daoFactory.commit();
}
}
Dao
package br.com.services.dao;
import java.util.List;
import org.hibernate.Session;
@SuppressWarnings("unchecked")
public class Dao<T> {
private final Session session;
private final Class classe;
public Dao(Session session, Class classe) {
this.session = session;
this.classe = classe;
}
public Session getSession() {
return this.session;
}
public void adiciona(T objecto) {
this.session.save(objecto);
}
public void remove(T objeto) {
this.session.delete(objeto);
}
public void atualiza(T objeto) {
this.session.merge(objeto);
}
public T procura(Long id) {
return (T) this.session.load(this.classe, id);
}
public List<T> listaTudo() {
return this.session.createCriteria(this.classe).list();
}
}
DaoFactory
package br.com.services.dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import br.com.services.modelo.Cliente;
import br.com.services.util.HibernateUtil;
public class DaoFactory {
private final Session session;
private Transaction transaction;
public DaoFactory() {
this.session = HibernateUtil.getSession();
}
public void beginTransaction() {
this.transaction = this.session.beginTransaction();
}
public void commit() {
this.transaction.commit();
this.transaction = null;
}
public boolean hasTransaction() {
return this.transaction != null;
}
public void rollback() {
this.transaction.rollback();
this.transaction = null;
}
public void close() {
this.session.close();
}
public Dao<Cliente> getClienteDao() {
return new Dao<Cliente>(this.session, Cliente.class);
}
}
Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/systoque</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="br.com.services.modelo.Cliente"/>
</session-factory>
</hibernate-configuration>
MAIN
package br.com.services.util;
import org.hibernate.Session;
import br.com.services.dao.DaoFactory;
import br.com.services.logic.ClienteLogic;
import br.com.services.modelo.Cliente;
public class Teste {
public static void main(String[] args) {
DaoFactory dao = new DaoFactory();
ClienteLogic logic = new ClienteLogic(dao);
Cliente cliente = new Cliente();
cliente.setNome("dfdf");
cliente.setRg("4987594");
logic.adiciona(cliente);
}
}