Oi pessoal consegui junto ao forum[color=red] configura /lib e hibernate.cfg.xml[/color] para usar hibernate mas na hora de grava os dados só grava[color=green] descricao e preco, fica faltando nome[/color]. no banco nome fica null???
resultado é este da main:
2 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.5.1-Final
117 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.1-Final
136 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
164 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
256 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
1370 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
1380 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
1380 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
1635 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
1642 [main] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
1871 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: br.com.caelum.goodbuy.modelo.Produto
1994 [main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity br.com.caelum.goodbuy.modelo.Produto on table Produto
2083 [main] INFO org.hibernate.cfg.AnnotationConfiguration - Hibernate Validator not found: ignoring
2241 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
2241 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
2241 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
2276 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/fj28
2276 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=admin, password=****}
2866 [main] INFO org.hibernate.cfg.SettingsFactory - RDBMS: MySQL, version: 5.0.18-nt-log
2866 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.12 ( Revision: ${bzr.revision-id} )
2944 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2965 [main] INFO org.hibernate.engine.jdbc.JdbcSupportLoader - Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
2968 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
2971 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2971 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
2971 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
2972 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
2972 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
2979 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
2979 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
2979 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
2981 [main] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
2981 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
2981 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
2981 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
2981 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
2981 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
3007 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
3007 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
3007 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
3007 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
3007 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
3007 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
3007 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
3007 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
3013 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
3014 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
3015 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
3015 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
3015 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
3015 [main] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
3184 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
3638 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
3648 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - Running hbm2ddl schema update
3648 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - fetching database metadata
3650 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - updating schema
3714 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - table found: fj28.produto
3714 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - columns: [id, preco, nome, descricao]
3714 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - foreign keys: []
3714 [main] INFO org.hibernate.tool.hbm2ddl.TableMetadata - indexes: [primary]
3715 [main] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - schema update complete
Hibernate:
insert
into
Produto
(descricao, preco)
values
(?, ?)
hibernate.cfg.xml :
<?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.username">admin</property>
<property name="hibernate.connection.password">azx321</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>
<property name="format_sql">true</property>
<mapping class="br.com.caelum.goodbuy.modelo.Produto" />
</session-factory>
</hibernate-configuration>
classe Produto:
package br.com.caelum.goodbuy.modelo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Produto {
@Id @GeneratedValue
private String nome;
private String descricao;
private Double preco;
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 String getNome() {
return nome;
}
public String getDescricao() {
return descricao;
}
public Double getPreco() {
return preco;
}
}
a main :
package br.com.caelum.goodbuy.testes;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
import br.com.caelum.goodbuy.modelo.Produto;
public class AdicaoDeProduto {
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();
}
}