Não consigo usar o Hibernate

2 respostas
leandrognaf

Pessoa estou tentando usar um exemplo com o Hibernate mas não estou tendo sucesso

segue o erro a baixo

og4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
        at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1494)
        at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:990)
        at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:69)
        at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
        at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)
        at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:69)
        at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
        at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:966)
        at teste.TesteDeSessao.main(TesteDeSessao.java:13)
Caused by: org.dom4j.DocumentException: Error on line 27 of document  : The markup in the document following the root element must be well-formed. Nested exception: The markup in the document following the root element must be well-formed.
        at org.dom4j.io.SAXReader.read(SAXReader.java:482)
        at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1484)
        ... 8 more
Java Result: 1
CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)

agora segue as classes

package teste;

import modelo.Produto;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class TesteDeSessao {

    public static void main(String[] args) {
        AnnotationConfiguration configuration = new AnnotationConfiguration();
        configuration.configure();

        SessionFactory factory = configuration.buildSessionFactory();
        Session session = factory.openSession();
        
        Produto produto = new Produto();
        produto.setNome("Prateleira");
        produto.setDescricao("Uma prateleira para colocar livros");
        produto.setPreco(35.90);
        
        
        Transaction tx = session.beginTransaction();
        session.save(produto);
        tx.commit();

    }
}
package modelo;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Entity;

@Entity
public class Produto {

    @Id
    @GeneratedValue
    private int id;
    private String nome, descricao;
    private Double preco;




    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 getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public Double getPreco() {
        return preco;
    }


    public void setPreco(Double preco) {
        this.preco = preco;
    }
    
}
<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : hibernate.cfg.xml
    Created on : 20 de Janeiro de 2011, 10:33
    Author     : User
    Description:
        Purpose of the document follows.
-->

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/fj28</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>



        <mapping class="modelo.Produto" />
    </session-factory>
</hibernate-configuration>

<arg>

</arg>

2 Respostas

joeroots

O problema está no seu hibernate.cfg
tenta colocar esse daqui, mudando as propriedades da sua aplicacao:

<?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/fj28</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password"></property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>

		
 		<mapping class="modelo.Produto" />
	</session-factory>

</hibernate-configuration>
leandrognaf

Deu o mesmo erro

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1494) at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:990) at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:69) at org.hibernate.cfg.Configuration.configure(Configuration.java:1428) at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972) at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:69) at org.hibernate.cfg.Configuration.configure(Configuration.java:1414) at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:966) at teste.TesteDeSessao.main(TesteDeSessao.java:13) Caused by: org.dom4j.DocumentException: Error on line 1 of document : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested exception: The processing instruction target matching "[xX][mM][lL]" is not allowed. at org.dom4j.io.SAXReader.read(SAXReader.java:482) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1484) ... 8 more Java Result: 1 CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)

Criado 20 de janeiro de 2011
Ultima resposta 20 de jan. de 2011
Respostas 2
Participantes 2