Acho que deve estar errando em algum passo que estou fazendo. Pois não consegui ainda fazer funcionar o Hibernate.
Estou usando o eclipse.
Primeiro eu criei uma Classe Cliente.
public class Cliente {
private int id;
private String nome;
private String telefone;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Depois eu criei o banco de dados no MySql.
CREATE TABLE Cliente (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
nome VARCHAR(255) NULL,
email VARCHAR(255) NULL,
telefone VARCHAR(255) NULL,
PRIMARY KEY(id)
)
Depois eu inseri os .jar.
Depois eu criei o Cliente.hbm.xml clicando em novo arquivo em branco e salvando.
<?xml version="1.0" encoding="UTF-8"?>
<class name="Cliente">
<!-- Identificador da Classe -->
<id name="id">
<generator class="increment"/>
</id>
<!-- Propriedades da Classe -->
<property name="nome"/>
<property name="telefone"/>
<property name="email"/>
</class>
Logo após eu criei um arquivo Hibernate.cfg.xml da mesma forma que criei cliente.
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/hibernacao?autoReconnect=true
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
1
</property>
<!-- Configuração do c3p0 -->
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">10</property>
<property name="hibernate.c3p0.idle_teste_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- Configurações de debug -->
<property name="show_sql">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.use_sql_comments">true</property>
<mapping resource="Cliente.hbm.xml"/>
</session-factory>
Depois eu criei a classe HibernateUtility.java.
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class HibernateUtility {
private static SessionFactory factory;
static {
try {
factory = new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
factory = null;
}
}
public static Session getSession() {
return factory.openSession();
}
}
Para finalizar criei a classe Teste.
import org.hibernate.*;
public class Teste {
public static void main(String[] args) {
Session sessao = HibernateUtility.getSession(); // abrindo secao
Transaction transaction = sessao.beginTransaction(); // iniciando uma transacao
Cliente cliente = new Cliente();
cliente.setNome("Gustavo Mantovani");
cliente.setTelefone("4432536238");
cliente.setEmail("gmantovani2005@gmail.com");
sessao.save(cliente);
transaction.commit(); // finalizando a transacao
sessao.close(); // fechando a sessao
}
}
Quando mandei rodar, apareceu o seguinte erro:
02/07/2008 23:03:11 org.hibernate.cfg.Environment
INFO: Hibernate 3.3.0.CR1
02/07/2008 23:03:11 org.hibernate.cfg.Environment
INFO: hibernate.properties not found
02/07/2008 23:03:11 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
02/07/2008 23:03:11 org.hibernate.cfg.Environment
INFO: using JDK 1.4 java.sql.Timestamp handling
02/07/2008 23:03:11 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
02/07/2008 23:03:11 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
02/07/2008 23:03:11 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : Cliente.hbm.xml
02/07/2008 23:03:16 org.hibernate.util.XMLHelper$ErrorLogger error
SEVERE: Error parsing XML: XML InputStream(1) The markup declarations contained or pointed to by the document type declaration must be well-formed.
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Cliente.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:579)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1598)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1566)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1545)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1519)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1439)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at HibernateUtility.(HibernateUtility.java:16)
at Teste.main(Teste.java:10)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:518)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:576)
… 8 more
Caused by: org.dom4j.DocumentException: Error on line 1 of document http://hibernate.sourcefource.net/hibernate-mapping-3.0.dtd : The markup declarations contained or pointed to by the document type declaration must be well-formed. Nested exception: The markup declarations contained or pointed to by the document type declaration must be well-formed.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:509)
… 9 more
Exception in thread “main” java.lang.NullPointerException
at HibernateUtility.getSession(HibernateUtility.java:29)
at Teste.main(Teste.java:10)