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!!!