Dúvida de Iniciante no Hibernate

4 respostas
Leonardo_Gloria

Fala rapazeada!
Comecando no hibernate aqui, to pegando os conceitos e talz. Dai to usando a apostila da Caelum, de hibernate vraptor e ajax…
Ai eu comecei a testar e minha classe de gerar banco ficou ok, ai fiz uns testes, os métdos save, delete, uptade estão ok, o problema é quando eu tento fazer um load. segue o codigo

<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/luthieria</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property> //apagado de proposito
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.format_sql">true</property>

    <mapping class="br.luthieria.modelo.Cliente"/>



  </session-factory>
</hibernate-configuration>

Classe com o main.

public class TestaAdiciona {
    public static void main(String[] args) {
        Configuration conf = new AnnotationConfiguration();
        conf.configure();
        SessionFactory factory = conf.buildSessionFactory();
        Session session = factory.openSession();

        Cliente cliente2 = new Cliente();
        Cliente cliente = new Cliente();
        //cliente.setId(1L);
        //cliente.setCpf(1111111111L);
        //cliente.setNome("Leonardo Gloria");
        //cliente.setEmail("[email removido]");

        Transaction t = session.beginTransaction();
        //session.save(cliente);
        cliente2 =(Cliente) session.load(Cliente.class,1L);
        t.commit();
        session.close();
    }
}

Saida:

17/09/2009 12:57:31 org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.1.GA
17/09/2009 12:57:31 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
17/09/2009 12:57:31 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
17/09/2009 12:57:31 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
17/09/2009 12:57:31 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
17/09/2009 12:57:31 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
17/09/2009 12:57:31 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
17/09/2009 12:57:32 org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
17/09/2009 12:57:32 org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: br.luthieria.modelo.Cliente
17/09/2009 12:57:32 org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity br.luthieria.modelo.Cliente on table Cliente
17/09/2009 12:57:32 org.hibernate.cfg.AnnotationConfiguration secondPassCompile
INFO: Hibernate Validator not found: ignoring
17/09/2009 12:57:32 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
17/09/2009 12:57:32 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
17/09/2009 12:57:32 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
17/09/2009 12:57:32 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/luthieria
17/09/2009 12:57:32 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root, password=****}
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: MySQL, version: 5.0.22-community-nt
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )
17/09/2009 12:57:33 org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
17/09/2009 12:57:33 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
17/09/2009 12:57:33 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
17/09/2009 12:57:33 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Echoing all SQL to stdout
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
17/09/2009 12:57:33 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
17/09/2009 12:57:33 org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
17/09/2009 12:57:33 org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
CONSTRUÍDO COM SUCESSO (tempo total: 5 segundos)

Pra finalizar classe Cliente:

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
 *
 * @author Administrador
 */
@Entity
public class Cliente implements Serializable {
    
    @Id @GeneratedValue
    private Long id;
    private String nome;
    private String email;
    private Long cpf;

    public Cliente() {
    }

    public Long getCpf() {
        return cpf;
    }

    public void setCpf(Long cpf) {
        this.cpf = cpf;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

}

O registro está OK no BD com o ID 1 msm.
Deve ser alguma bestera que sinceramente não to sendo capaz de achar!
Agradeço desde já!

4 Respostas

Cherubini

para realizar consultas nao precisa ter transacao nem commit!!!

é so tirar q vai rodar!

Leonardo_Gloria

Pow me brother, tirei aki e o código ficou assim

public static void main(String[] args) {
        Configuration conf = new AnnotationConfiguration();
        conf.configure();
        SessionFactory factory = conf.buildSessionFactory();
        Session session = factory.openSession();

        Cliente cliente2 = new Cliente();
        Cliente cliente = new Cliente();
        cliente.setId(1L);
        cliente.setCpf(12496329784L);
        cliente.setNome("Leonardo Gloria");
        cliente.setEmail("[email removido]");

        //Transaction t = session.beginTransaction();
        //session.save(cliente);
        cliente2 =(Cliente) session.load(Cliente.class,1L);
        //session.save(cliente);
        //t.commit();
        session.close();

Deu a msm saida…
Idéias?

Andre_Brito

Cara, mas onde está o erro?

Leonardo_Gloria

para realizar consultas nao precisa ter transacao nem commit!!!

é so tirar q vai rodar!

Galera, perdão, o lance é que eu tirei mas retestei com o código antigo. Problema Resolvido!

Obrigado a todos!

Criado 17 de setembro de 2009
Ultima resposta 18 de set. de 2009
Respostas 4
Participantes 3