Bom dia pessoal esse é meu primeiro poste aqui, estou tendo dificuldades em criar uma tabela e acessar ela no meu banco de dados, porem o Hibernate faz conexão normal com o banco. Segue meus códigos.
Se alguém pude-se me ajudar eu agradeceria muito. Obrigado pela atenção de todos.
Minha classe de contato, minha entidade.
package br.com.crudannotations;
import java.sql.Date;
import javax.persistence.*;
@Entity
@Table(name = "contato")
public class Contato{
@Id
@GeneratedValue
@Column(name = "codigo")
private Integer codigo;
@Column(name = "nome", length = 50, nullable = true)
private String nome;
@Column(name = "telefone", length = 50, nullable = true)
private String telefone;
@Column(name = "email", length = 50, nullable = true)
private String email;
@Column(name = "dt_cad", nullable = true)
private Date dataCadastro;
@Column(name = "obs", nullable = true)
private String observacao;
public Integer getCodigo() {
return codigo;
}
public void setCodigo(Integer codigo) {
this.codigo = codigo;
}
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;
}
public Date getDataCadastro() {
return dataCadastro;
}
public void setDataCadastro(Date dataCadastro) {
this.dataCadastro = dataCadastro;
}
public String getObservacao() {
return observacao;
}
public void setObservacao(String observacao) {
this.observacao = observacao;
}
}
Minha classe de crud e meu teste de inserção no banco
package br.com.crudannotations;
import java.sql.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import br.com.hibernate.conexao.HibernateUtil;
public class ContatoCrudAnnotations {
private Session sessao;
public ContatoCrudAnnotations(Session sessao) {
this.sessao = sessao;
}
public void salvar(Contato contato) {
sessao.save(contato);
}
public void atualizar(Contato contato) {
sessao.update(contato);
}
public void excluir(Contato contato) {
sessao.delete(contato);
}
public List<Contato> listar() {
Query consulta = sessao.createQuery("from Contato");
return consulta.list();
}
public Contato buscaContato(int valor) {
Query consulta = sessao.createQuery("from Contato where codigo = :parametro");
consulta.setInteger("parametro", valor);
return (Contato) consulta.uniqueResult();
}
public static void main(String[] args) {
Session sessao = HibernateUtil.getSessionFactory().openSession();
Transaction transacao = sessao.beginTransaction();
ContatoCrudAnnotations contatoCrud = new ContatoCrudAnnotations(sessao);
Contato contato1 = new Contato();
contato1.setNome("wallyson");
contato1.setTelefone("85987099018");
contato1.setEmail("wallyson2712@hotmail.com");
contato1.setDataCadastro(new Date(System.currentTimeMillis()));
contato1.setObservacao("Primeiro cliente teste");
contatoCrud.salvar(contato1);
contato1.setObservacao("Cliente teste 1");
contatoCrud.atualizar(contato1);
Contato contato2 = new Contato();
contato2.setNome("Gabriele");
contato2.setTelefone("85987099018");
contato2.setEmail("gabriele@gmail.com");
contato2.setDataCadastro(new Date(System.currentTimeMillis()));
contato2.setObservacao("Segundo cliente teste");
contatoCrud.salvar(contato2);
System.out.println("Total de registros cadastrados: "+contatoCrud.listar().size());
contatoCrud.excluir(contato1);
transacao.commit();
System.out.println("Total de registros cadastrados: "+contatoCrud.listar().size());
}
}
Meu arquivo hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/agenda</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="current_session_context_class">thread</property>
<property name="c3po.min_size">5</property>
<property name="c3po.max_size">20</property>
<property name="c3po.timeout">300</property>
<property name="c3po.max_statements">50</property>
<property name="c3po.idle_test_period">3000</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="generate_statistics">true</property>
<property name="use_sql_comments">true</property>
<mapping class="br.com.crudannotations.Contato"/>
</session-factory>
</hibernate-configuration>
Meu log de erro
ago 01, 2016 9:29:43 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
ago 01, 2016 9:29:43 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.4.Final}
ago 01, 2016 9:29:43 AM org.hibernate.cfg.Environment
INFO: HHH000206: hibernate.properties not found
ago 01, 2016 9:29:43 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
ago 01, 2016 9:29:44 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
ago 01, 2016 9:29:44 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
ago 01, 2016 9:29:44 AM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
ago 01, 2016 9:29:44 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
ago 01, 2016 9:29:44 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/agenda]
ago 01, 2016 9:29:44 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=***}
ago 01, 2016 9:29:44 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
ago 01, 2016 9:29:44 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
ago 01, 2016 9:29:44 AM org.hibernate.dialect.Dialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
ago 01, 2016 9:29:44 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
ago 01, 2016 9:29:44 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate:
/ insert br.com.crudannotations.Contato
*/ insert
into
contato
(dt_cad, email, nome, obs, telefone)
values
(?, ?, ?, ?, ?)
ago 01, 2016 9:29:45 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1146, SQLState: 42S02
ago 01, 2016 9:29:45 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Table ‘agenda.contato’ doesn’t exist
Exception in thread “main” org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:80)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:190)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:97)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:488)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:193)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:177)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:212)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
at br.com.crudannotations.ContatoCrudAnnotations.salvar(ContatoCrudAnnotations.java:21)
at br.com.crudannotations.ContatoCrudAnnotations.main(ContatoCrudAnnotations.java:55)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘agenda.contato’ doesn’t exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:187)
… 23 more