Primeiramente gostaria de agradecer pela rápida resposta.
Os códigos são os seguintes:
Mais uma vez obrigado
====> 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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/basedados</property>
<!--property name="connection.datasource">java:comp/env/jdbc/basedados</property-->
<property name="connection.username">postgres</property>
<property name="connection.password">postgres</property>
<property name="connection.autocommit">true</property>
<property name="show_sql">true</property>
<property name="create-drop">false</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!--property name="dialect">org.hibernate.dialect.HSQLDialect</property-->
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!--mapping class="pacote.Usuario" /-->
<mapping resource="pacote/Usuario.hbm.xml"/>
</session-factory>
</hibernate-configuration>
====> Usuario.hbm.xml
<?xml version=“1.0”?>
<!DOCTYPE hibernate-mapping PUBLIC “-//Hibernate Mapping DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
<hibernate-mapping>
<class name=“pacote.Usuario” table=“tb_usuario”>
<id name=“UsCod” column=“USCOD” type=“int”>
<generator class=“increment”/>
</id>
<property name=“UsSenha” column=“USSENHA” type=“string”/>
<property name=“UsNome” column=“USNOME” type=“string”/>
<property name=“UsEmail” column=“USEMAIL” type=“string”/>
</class>
</hibernate-mapping>
====> UsuarioDAO.java
package pacote;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class UsuarioDAO {
private SessionFactory factory;
public UsuarioDAO() throws Exception {
factory = new Configuration().configure().buildSessionFactory();
}
public void UsInserir(Usuario us) throws Exception {
Session session = factory.openSession();
session.save(us);
session.flush();
session.close();
}
public void UsAlterar(Usuario us) throws Exception {
Session session = factory.openSession();
session.update(us);
session.flush();
session.close();
}
public void UsExcluir(Usuario us) throws Exception {
Session session = factory.openSession();
session.delete(us);
session.flush();
session.close();
}
public List getListaUsuario() {
Session session = factory.openSession();
String sql = "from Usuario";
List listaUsuarios = session.createQuery(sql).list();
return listaUsuarios;
}
}
====> Usuario.java
package pacote;
public class Usuario {
private int usCod;
private String usSenha;
private String usNome;
private String usEmail;
public Usuario() {
}
public Usuario(String usSenha, String usNome, String usEmail) {
this.setUsSenha(usSenha);
this.setUsNome(usNome);
this.setUsEmail(usEmail);
}
public String getUsEmail() {
return usEmail;
}
public void setUsEmail(String usEmail) {
this.usEmail = usEmail;
}
public String getUsNome() {
return usNome;
}
public void setUsNome(String usNome) {
this.usNome = usNome;
}
public String getUsSenha() {
return usSenha;
}
public void setUsSenha(String usSenha) {
this.usSenha = usSenha;
}
public int getUsCod() {
return usCod;
}
public void setUsCod(int usCod) {
this.usCod = usCod;
}
}
====> TesteHibernate.java
package pacote;
import java.util.List;
public class TesteHibernate {
public static void main(String[] args) throws Exception {
try {
String senha = "abc";
String nome = "Nome";
String email = "email@email.com.br";
UsuarioDAO dao = new UsuarioDAO();
Usuario usuario = new Usuario(senha, nome, email);
dao.UsInserir(usuario);
System.out.println("Registro inserido com sucesso!!!");
List lista = dao.getListaUsuario();
for (int i = 0; i < lista.size(); i++) {
Usuario user = (Usuario)lista.get(i);
System.out.println(user.getUsEmail());
}
} catch (Exception e) {
System.out.println("Não foi possivel, Erro: " + e.getMessage());
e.printStackTrace();
}
}
}