Olá pessoal, estou tentando fazer uma aplicação simples para ver como funciona o hibernate... usando o mesmo NetBeans + postgres e estou com problemas com o mapeamento... segue os códigos
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.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/Hibernate</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">12345</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
</session-factory>
</hibernate-configuration>
hibernate.dialect org.postgresql.Driver
hibernate.connection.url jdbc:postgresql://localhost:5432/postgres
hibernate.connection.username postgres
hibernate.connection.password 12345
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=info, stdout
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate Mapping DTD 3.0//EN"
"hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Usuario" table="db_usuarios">
<id name="UsCod" column="USCOD" type="string">
< generator class="assigned"/>
</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>
log4j.logger.org.hibernate.test=info
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.somepackage.eg">
<class name="Foo" table=?FooTable?>
<id name="id" type=?java.lang.Long?>
<generator class=?sequence?/>
</id>
</class>
</hibernate-mapping>
Esses sao os arquivos de configurações e estão na seguinte estrutura
Hibernate (Projeto)
hibernate-mapping-3.0.dtd
hibernate.cfg.xml
build
nbproject
src
Teste.java
Usuario.java
UsuarioDAO.java
hibernate.properties
log4j.properties
Usuario.hbm.xml
test
As classes
Teste.javapublic class Teste {
public static void main(String[] args) throws Exception {
try
{
String log = "login";
String senha = "abc";
String nome = "Rafael";
String email = "[email removido]";
UsuarioDAO dao = new UsuarioDAO();
Usuario usuario = new Usuario(log,senha,nome,email);
dao.UsInserir(usuario);
System.out.println("Registro inserido com sucesso!!!");
}
catch(Exception e)
{
System.out.println("Não foi possivel, Erro: " + e.getMessage());
}
}
}
public class Usuario {
private String usCod;
private String usSenha;
private String usNome;
private String usEmail;
public Usuario(){
}
public Usuario(String usCod, String usSenha, String usNome, String usEmail) {
this.setUsCod(usCod);
this.setUsSenha(usSenha);
this.setUsNome(usNome);
this.setUsEmail(usEmail);
}
public String getUsCod() {
return usCod;
}
public void setUsCod(String usCod) {
this.usCod = usCod;
}
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;
}
}
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Expression;
public class UsuarioDAO{
private SessionFactory factory;
public UsuarioDAO() throws Exception{
factory = new Configuration().addClass(Usuario.class).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();
}
}
Quando eu Executo o projeto recebo a seguinte mensagem...
init:
deps-jar:
compile-single:
run-single:
13/07/2009 16:49:22 org.hibernate.cfg.Environment
INFO: Hibernate 3.2.5
13/07/2009 16:49:22 org.hibernate.cfg.Environment
INFO: hibernate.properties not found
13/07/2009 16:49:22 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
13/07/2009 16:49:22 org.hibernate.cfg.Environment
INFO: using JDK 1.4 java.sql.Timestamp handling
13/07/2009 16:49:22 org.hibernate.cfg.Configuration addClass
INFO: Reading mappings from resource: Usuario.hbm.xml
13/07/2009 16:49:22 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource: Usuario.hbm.xml
Não foi possivel, Erro: resource: Usuario.hbm.xml not found
CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)
a tabela db_usuario está criada no meu postgres normal... eu adicionei a biblioteca do hibernate no netbeans, procurei porai e nao consegui resolver o problema.... aguardo a ajuda de voces grato !
