Hibernate no Eclipse

6 respostas
Gneto

Olá meus amigos!

Estou com dificuldades em trabalhar com o hibernate. Estou seguindo a apostila fj28 da caelum mais mesmo assim não estou conseguindo sucesso.

Segue o código:

package curso.teste;

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

import curso.orlando.Produto;

public class AdicaoDeProduto {
	   public static void main(String args[]){
		   
		   AnnotationConfiguration configuration = new AnnotationConfiguration();
		   configuration.configure();//esta lendo o arquivo hibernate.cfg.xml
		  
		   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 curso.orlando;

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

@Entity
public class Produto{
	
	@Id @GeneratedValue
	private Long id;
	
	private String nome;
	
	private String descricao;
	
	private Double preco;
	
	public void setId(Long ID){
		this.id = ID;
	}
	public void setnome(String NOME){
		this.nome = NOME;
	}
	public void setdescricao(String DESCRICAO){
		this.descricao = DESCRICAO;
	}
	public void setpreco(Double PRECO){
		this.preco = PRECO;
	}
	public Long getId(){
		return this.id;
	}
	public String getNome(){
		return this.nome;	
	}
	public String getDescricao(){
		return this.descricao;
	}
	public Double getPreco(){
		return this.preco;
	}
}

O arquivo hibernate.cfg.xml;

<!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">orlando</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <property name="hibernate.hbm2dd1.auto">update</property>
        
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>        
        <mapping class="curso.orlando.Produto"/>
    </session-factory>
</hibernate-configuration>

O erro que esta ocorrendo:

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.exception.SQLGrammarException: Cannot open connection

at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)

at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)

at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)

at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)

at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)

at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)

at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354)

at curso.teste.AdicaoDeProduto.main(AdicaoDeProduto.java:24)

Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ??? at line 1

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2642)

at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1818)

at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3544)

at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2156)

at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:781)

at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:348)

at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284)

at java.sql.DriverManager.getConnection(libgcj.so.10)

at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)

at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)

5 more

Não vejo nenhum tipo de erro de sintaxe nos códigos alguem pode me ajudar!!!

6 Respostas

tiago.vt

amigo da 1 verifica nos jars se voce adiciono corretamente todos que precisa.

e posta os que você adiciono.

H

Eu já tive um problema parecido com esse. Altera a tua classe AdicaoDeProduto e testa ai.

public class AdicaoDeProduto { 

	public static void main(String args[]){
		   
		Configuration 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();
}
Gneto

Ja verifiquei meus .jars são eles:

hibernate-annotations.jar
hibernate-testing.jar
hibernate3.jar
log4j.jar
antlr-2.7.6.jar
dom4j.jar
jta-1.1.jar
hibernate-entitymanager.jar
hibernate-commons-annotations.jar
hibernate-core.jar
slf4j-api.jar
commons-collections-3.1.jar
ejb3-persistence.jar
javassist-3.9.0.GA.jar
slf4j-api-1.5.8.jar
mysql-connector-java-5.1.10-bin.jar

tiago.vt

Gneto:
Ja verifiquei meus .jars são eles:

hibernate-annotations.jar
hibernate-testing.jar
hibernate3.jar
log4j.jar
antlr-2.7.6.jar
dom4j.jar
jta-1.1.jar
hibernate-entitymanager.jar
hibernate-commons-annotations.jar
hibernate-core.jar
slf4j-api.jar
commons-collections-3.1.jar
ejb3-persistence.jar
javassist-3.9.0.GA.jar
slf4j-api-1.5.8.jar
mysql-connector-java-5.1.10-bin.jar

experimenta coloca esse jar slf4j-log

H

Você tentou fazer a alteração que eu te falei?

LPJava

olha para saber se tem algum arquivo faltando leia o arquivo _README.txt que vem dentro de lib quando vc baixa o hibernate, la tem tudo explicado quais sao required.

veja esse post: http://blog.camilolopes.com.br/?p=1228 esqueça a parte de JSF e analise somente a parte de hibernate e veja onde está errando. tem outros posts sobre hibernate tb.

eu recomendo o livro: http://www.amazon.com/Beginning-Hibernate-Novice-Professional/dp/[telefone removido] vc pode comprar ou achar ai na net. eu estou lendo o livro e realmente eh fantatico para quem quer aprender, bem pratico e com uma boa dose de teorica para nao ficar so na pratica sem saber o que está fazendo.

abracos,

Criado 3 de fevereiro de 2010
Ultima resposta 9 de fev. de 2010
Respostas 6
Participantes 4