Inserir via Hibernate URGENTE!

3 respostas
P

Amigos, não estou conseguindo salvar com Hibernate.

Abaixo minha classe que gera a conexão:
package br.com.allware.classes.fabricaConexao;

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

import br.com.allware.classes.modelos.Produto;

public class ConexaoBD {
	
	private static SessionFactory factory;
	
	static{
		AnnotationConfiguration cfg = new AnnotationConfiguration();
		cfg.addAnnotatedClass(Produto.class);
		factory = cfg.buildSessionFactory();
	}
	
	public Session getConexao(){
		return factory.openSession();
	}

}

Minha classe que insere:

package br.com.allware.testes.geral;

import br.com.allware.classes.fabricaConexao.ConexaoBD;
import br.com.allware.classes.modelos.Produto;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
import org.hibernate.tool.hbm2ddl.SchemaExport;

@SuppressWarnings("unused")
public class Teste {
	public static void main(String[] args) {
		
		/*AnnotationConfiguration cfg = new AnnotationConfiguration();
		cfg.addAnnotatedClass(Produto.class);
		new SchemaExport(cfg).create(true, true);*/
		
		Produto p = new Produto();
		p.setNome("Banana");
		p.setPreco(10);
		
		Session session = new ConexaoBD().getConexao();
		session.save(p);
		session.close();
	}
}

O Show_SQL está True.

Quando rodo, no meu console é exibido o seguinte:

16 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.3.1.GA
31 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA
31 [main] INFO org.hibernate.cfg.Environment - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.postgresql.Driver, hibernate.max_fetch_depth=1, hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.format_sql=true, hibernate.query.substitutions=yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=postgres, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:postgresql://localhost:5432/allware, hibernate.show_sql=true, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=****, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=1}
31 [main] INFO org.hibernate.cfg.Environment - using java.io streams to persist binary types
31 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
47 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
219 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: br.com.allware.classes.modelos.Produto
250 [main] INFO org.hibernate.cfg.annotations.EntityBinder - Bind entity br.com.allware.classes.modelos.Produto on table Produto
312 [main] INFO org.hibernate.validator.Version - Hibernate Validator 3.1.0.GA
391 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
391 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 1
391 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
422 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.postgresql.Driver at URL: jdbc:postgresql://localhost:5432/allware
422 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=postgres, password=****}
531 [main] INFO org.hibernate.cfg.SettingsFactory - RDBMS: PostgreSQL, version: 8.1.3
531 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 8.4 JDBC3 (build 701)
562 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.PostgreSQLDialect
562 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
562 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
562 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
562 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
562 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
562 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: enabled
562 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
562 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): disabled
562 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
562 [main] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 1
562 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
562 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
562 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
562 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
562 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
578 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
578 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {yes='Y', no='N'}
578 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
578 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
578 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
578 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
578 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
578 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region prefix: hibernate.test
578 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
578 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
578 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
578 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
578 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
578 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
641 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
844 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
Hibernate: 
    select
        nextval ('hibernate_sequence')

Não está exibindo o INSERT no Console. Acho que é por isso que não tá inserindo na tabela de produtos.

Já estou conseguindo gerar as tabelas via hibernate, mais não consigo inserir na tabela.

3 Respostas

romarcio
@SuppressWarnings("unused")
public class Teste {
	public static void main(String[] args) {

		Produto p = new Produto();
		p.setNome("Banana");
		p.setPreco(10);

        Transaction transaction = null;

		Session session = new ConexaoBD().getConexao();
        
        transaction = session.beginTransaction();
        
		session.save(p);
        
        transaction.commit();
        
		session.close();
	}
}
P

Obrigado era isso mesmo.

Abraços!

romarcio

pw2tecnologia:
Obrigado era isso mesmo.

Abraços!

Blz, edita seu 1º Post e coloca como resolvido no titulo: Inserir via Hibernate URGENTE !!! [RESOLVIDO]

Criado 10 de agosto de 2010
Ultima resposta 10 de ago. de 2010
Respostas 3
Participantes 2