package teste;
public class Principal {
/**
* @param args
*/
public static void main(String[] args) {
Cliente c1 = new Cliente();
c1.setNome("Joaquim");
c1.setIdade(new Long(12));
Cliente c2 = new Cliente();
c2.setNome("Renata");
c2.setIdade(new Long(13));
ClienteDAO clienteDAO = new ClienteDAO();
clienteDAO.salvar(c1);
clienteDAO.salvar(c2);
}
}
package teste;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
package teste;
import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name = "cliente")
public class Cliente implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "nome_cliente", nullable = false, length=60)
private String nome;
@Column(name = "idade_cliente", nullable = false, length = 5)
private Long idade;
public Cliente(){
}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public Long getIdade() {
return idade;
}
public void setIdade(Long idade) {
this.idade = idade;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
package teste;
import org.hibernate.Session;
import org.hibernate.mapping.List;
import teste.Cliente;
import teste.HibernateUtil;
public class ClienteDAO {
private Session session;
public ClienteDAO(){
}
public void salvar(Cliente cli){
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(cli);
session.getTransaction().commit();
}
public List listar(){
session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
List l1 = (List) session.createQuery("from Cliente").list();
session.getTransaction().commit();
return l1;
}
}
<?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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/teste</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<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>
<mapping class="teste.Cliente"/>
</session-factory>
</hibernate-configuration>
OLHA OS ERROS:
Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at teste.HibernateUtil.
at teste.ClienteDAO.salvar(ClienteDAO.java:13)
at teste.Principal.main(Principal.java:16)
Caused by: org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1528)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1035)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1017)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1448)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1011)
at teste.HibernateUtil.
... 2 more
Caused by: org.dom4j.DocumentException: Error on line 24 of document : The element type "session-factory" must be terminated by the matching end-tag "". Nested exception: The element type "session-factory" must be terminated by the matching end-tag "".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1518)
... 10 more
Alguem pode me dar um help?!