Busca com hibernate retornando NULL?

0 respostas
java
T

Estou estudando e fazendo um projeto com hibernate, mas na hora de fazer a busca por código o return é null, mas no banco tem fabricantes cadastrados, Alguém pode me ajudar, desde já agradeço!

package br.com.drogaria.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 *
 * @author Thiago Silva
 */
@Entity
@Table(name = "tbl_fabricantes")
public class Fabricante implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "fab_codigo")
    private Long codigo;

    @Column(name = "fab_descricao", length = 50, nullable = false)
    private String descricao;

    public Long getCodigo() {
        return codigo;
    }

    public void setCodigo(Long codigo) {
        this.codigo = codigo;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    @Override
    public String toString() {
        return "Fabricante{" + "codigo=" + codigo + ", descricao=" + descricao + '}';
    }

    
}






package br.com.dorgaria.dao;

import br.com.drogaria.domain.Fabricante;
import br.com.drogaria.util.HibernateUtil;
import java.util.List;
import org.hibernate.Query;

import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Thiago Silva
 */
public class FabricanteDao {

    public void salvar(Fabricante fabricante) {
        Session sessao = null;
        Transaction transacao = null;

        try {
            sessao = HibernateUtil.getSessionFactory().openSession();
            transacao = sessao.beginTransaction();
            sessao.save(fabricante);
            transacao.commit();
        } catch (RuntimeException ex) {
            if (transacao != null) {
                transacao.rollback();
            }
            throw ex;
        }
    }

    public List<Fabricante> listar() {
        Session sessao = HibernateUtil.getSessionFactory().openSession();

        List<Fabricante> fabricantes = null;
        try {
            Query consulta = sessao.createQuery("From Fabricante");
            fabricantes = consulta.list();
        } catch (RuntimeException ex) {
            throw ex;
        } finally {
            try {
                sessao.close();
            } catch (Exception e) {
                throw e;
            }
        }
        return fabricantes;
    }

    public Fabricante buscarPorCodigo(Long codigo) {
        Session sessao = HibernateUtil.getSessionFactory().openSession();

        Fabricante fabricante = null;
        try {
            Query consulta = (Query) sessao.createQuery("Select fabricante FROM Fabricante fabricante WHERE fabricante.codigo = :codigo") ;
            consulta.setLong("codigo", codigo);
            fabricante = (Fabricante) consulta.uniqueResult();
        } catch (RuntimeException ex) {
            throw ex;
        } finally {
            sessao.close();
        }
        return fabricante;
    }

    public void excluir(Fabricante fabricante) {

        try {
            Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction transacao = sessao.beginTransaction();
            sessao.delete(fabricante);
            transacao.commit();
        } catch (RuntimeException ex) {

            throw ex;
        }
    }

    public void editar(Fabricante fabricante) {
        Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction transacao = null;

        try {
            transacao = sessao.beginTransaction();
//            Fabricante fabricanteEditar = buscarPorCodigo(fabricante.getCodigo());
//            fabricanteEditar.setDescricao(fabricante.getDescricao());
//            sessao.update(fabricanteEditar);
            sessao.update(fabricante);
            transacao.commit();
        } catch (RuntimeException ex) {
            if (transacao != null) {
                transacao.rollback();
            }
            throw ex;
        } finally {
            sessao.close();
        }
    }

}


package br.com.drogaria.teste;

import br.com.dorgaria.dao.FabricanteDao;
import br.com.drogaria.domain.Fabricante;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;

/**
 *
 * @author Thiago Silva
 */
public class FabricanteDaoteste {

   

    @Test
    public void buscarPorCodigo() {

        FabricanteDao dao = new FabricanteDao();
        Fabricante f = dao.buscarPorCodigo(2l);

        System.out.println(f);
    }
WARNING: multiple versions of ant detected in path for junit

jar:file:/C:/Program%20Files/NetBeans%208.1/extide/ant/lib/ant.jar!/org/apache/tools/ant/Project.class

and jar:file:/C:/Program%20Files/glassfish-4.1.1/glassfish/modules/ant.jar!/org/apache/tools/ant/Project.class

Testsuite: br.com.drogaria.teste.FabricanteDaoteste

jun 02, 2017 7:39:40 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager 

INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}

jun 02, 2017 7:39:40 PM org.hibernate.Version logVersion

INFO: HHH000412: Hibernate Core {4.3.1.Final}

jun 02, 2017 7:39:40 PM org.hibernate.cfg.Environment 

INFO: HHH000206: hibernate.properties not found

jun 02, 2017 7:39:40 PM org.hibernate.cfg.Environment buildBytecodeProvider

INFO: HHH000021: Bytecode provider name : javassist

jun 02, 2017 7:39:40 PM org.hibernate.cfg.Configuration configure

INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml

jun 02, 2017 7:39:40 PM org.hibernate.cfg.Configuration getConfigurationInputStream

INFO: HHH000040: Configuration resource: /hibernate.cfg.xml

jun 02, 2017 7:39:40 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity

WARN: HHH000223: Recognized obsolete hibernate namespace <a href="http://hibernate.sourceforge.net/">http://hibernate.sourceforge.net/</a>. Use namespace <a href="http://www.hibernate.org/dtd/">http://www.hibernate.org/dtd/</a> instead. Refer to Hibernate 3.6 Migration Guide!

jun 02, 2017 7:39:40 PM org.hibernate.cfg.Configuration doConfigure

INFO: HHH000041: Configured SessionFactory: null

jun 02, 2017 7:39:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)

jun 02, 2017 7:39:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/drogaria?zeroDateTimeBehavior=convertToNull]

jun 02, 2017 7:39:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH000046: Connection properties: {user=root, password=****}

jun 02, 2017 7:39:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH000006: Autocommit mode: false

jun 02, 2017 7:39:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

INFO: HHH000115: Hibernate connection pool size: 10 (min=1)

jun 02, 2017 7:39:42 PM org.hibernate.dialect.Dialect 

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect

jun 02, 2017 7:39:43 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService

INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)

jun 02, 2017 7:39:43 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory 

INFO: HHH000397: Using ASTQueryTranslatorFactory

jun 02, 2017 7:39:43 PM org.hibernate.validator.internal.util.Version 

INFO: HV000001: Hibernate Validator 5.1.2.Final

jun 02, 2017 7:39:44 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute

INFO: HHH000228: Running hbm2ddl schema update

jun 02, 2017 7:39:44 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute

INFO: HHH000102: Fetching database metadata

jun 02, 2017 7:39:44 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute

INFO: HHH000396: Updating schema

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000261: Table found: drogaria.tbl_fabricantes

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000037: Columns: [fab_descricao, fab_codigo]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000108: Foreign keys: []

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000126: Indexes: [primary]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000261: Table found: drogaria.tbl_funcionario

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000037: Columns: [fun_cpf, fun_nome, fun_senha, fun_codigo, fun_funcao]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000108: Foreign keys: []

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000126: Indexes: [uk_gnml6u13myrhd6hbwfbanv4ti, primary]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000261: Table found: drogaria.tbl_itens

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000037: Columns: [codigo, tbl_vendas_ven_codigo, ite_valor_parcial, ite_quantidade, tbl_produto_pro_codigo]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000108: Foreign keys: [fk5hf7d59snok6yh63jf3i3w527, fk_nk6bdj58rd66e7518sxt4vbcu, fk_b4p7wc6k2vfcnxohku8m8ppd7, fk4ppfm1nif5t9fevx0po1ap2oo]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000126: Indexes: [fk5hf7d59snok6yh63jf3i3w527, fk4ppfm1nif5t9fevx0po1ap2oo, primary]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000261: Table found: drogaria.tbl_produto

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000037: Columns: [pro_codigo, pro_decricao, tbl_fabricante_fab_codigo, pro_quantidade, pro_preco]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000108: Foreign keys: [fkrgq3f3ehhqevn8bet6vua9ekc, fk_lqtyreeqcrxso64rf5lhriwuh]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000126: Indexes: [fkrgq3f3ehhqevn8bet6vua9ekc, primary]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000261: Table found: drogaria.tbl_venda

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000037: Columns: [tbl_funcionario_fun_codigo, ven_horario, ven_valor_total, ven_codigo]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000108: Foreign keys: [fkbm6cs0iafro7arjtct2hlv1b8, fk_1hxl6xntuebqju9wseas9uiwy]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000126: Indexes: [fkbm6cs0iafro7arjtct2hlv1b8, primary]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute

INFO: HHH000232: Schema update complete

Hibernate:

select

fabricante0_.fab_codigo as fab_codi1_0_,

fabricante0_.fab_descricao as fab_desc2_0_

from

tbl_fabricantes fabricante0_

where

fabricante0_.fab_codigo=?

null

Tests run: 5, Failures: 0, Errors: 0, Skipped: 4, Time elapsed: 7,035 sec

------------- Standard Output ---------------
Hibernate:
select
fabricante0_.fab_codigo as fab_codi1_0_,
fabricante0_.fab_descricao as fab_desc2_0_
from
tbl_fabricantes fabricante0_
where
fabricante0_.fab_codigo=?
null


------------- Standard Error -----------------

jun 02, 2017 7:39:40 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager 

INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}

jun 02, 2017 7:39:40 PM org.hibernate.Version logVersion

INFO: HHH000412: Hibernate Core {4.3.1.Final}

jun 02, 2017 7:39:40 PM org.hibernate.cfg.Environment 

INFO: HHH000206: hibernate.properties not found

jun 02, 2017 7:39:40 PM org.hibernate.cfg.Environment buildBytecodeProvider

INFO: HHH000021: Bytecode provider name : javassist

jun 02, 2017 7:39:40 PM org.hibernate.cfg.Configuration configure

INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml

jun 02, 2017 7:39:40 PM org.hibernate.cfg.Configuration getConfigurationInputStream

INFO: HHH000040: Configuration resource: /hibernate.cfg.xml

jun 02, 2017 7:39:40 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity

WARN: HHH000223: Recognized obsolete hibernate namespace <a href="http://hibernate.sourceforge.net/">http://hibernate.sourceforge.net/</a>. Use namespace <a href="http://www.hibernate.org/dtd/">http://www.hibernate.org/dtd/</a> instead. Refer to Hibernate 3.6 Migration Guide!

jun 02, 2017 7:39:40 PM org.hibernate.cfg.Configuration doConfigure

INFO: HHH000041: Configured SessionFactory: null

jun 02, 2017 7:39:40 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)

jun 02, 2017 7:39:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/drogaria?zeroDateTimeBehavior=convertToNull]

jun 02, 2017 7:39:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH000046: Connection properties: {user=root, password=****}

jun 02, 2017 7:39:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH000006: Autocommit mode: false

jun 02, 2017 7:39:41 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

INFO: HHH000115: Hibernate connection pool size: 10 (min=1)

jun 02, 2017 7:39:42 PM org.hibernate.dialect.Dialect 

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect

jun 02, 2017 7:39:43 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService

INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)

jun 02, 2017 7:39:43 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory 

INFO: HHH000397: Using ASTQueryTranslatorFactory

jun 02, 2017 7:39:43 PM org.hibernate.validator.internal.util.Version 

INFO: HV000001: Hibernate Validator 5.1.2.Final

jun 02, 2017 7:39:44 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute

INFO: HHH000228: Running hbm2ddl schema update

jun 02, 2017 7:39:44 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute

INFO: HHH000102: Fetching database metadata

jun 02, 2017 7:39:44 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute

INFO: HHH000396: Updating schema

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000261: Table found: drogaria.tbl_fabricantes

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000037: Columns: [fab_descricao, fab_codigo]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000108: Foreign keys: []

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000126: Indexes: [primary]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000261: Table found: drogaria.tbl_funcionario

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000037: Columns: [fun_cpf, fun_nome, fun_senha, fun_codigo, fun_funcao]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000108: Foreign keys: []

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000126: Indexes: [uk_gnml6u13myrhd6hbwfbanv4ti, primary]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000261: Table found: drogaria.tbl_itens

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000037: Columns: [codigo, tbl_vendas_ven_codigo, ite_valor_parcial, ite_quantidade, tbl_produto_pro_codigo]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000108: Foreign keys: [fk5hf7d59snok6yh63jf3i3w527, fk_nk6bdj58rd66e7518sxt4vbcu, fk_b4p7wc6k2vfcnxohku8m8ppd7, fk4ppfm1nif5t9fevx0po1ap2oo]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000126: Indexes: [fk5hf7d59snok6yh63jf3i3w527, fk4ppfm1nif5t9fevx0po1ap2oo, primary]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000261: Table found: drogaria.tbl_produto

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000037: Columns: [pro_codigo, pro_decricao, tbl_fabricante_fab_codigo, pro_quantidade, pro_preco]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000108: Foreign keys: [fkrgq3f3ehhqevn8bet6vua9ekc, fk_lqtyreeqcrxso64rf5lhriwuh]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000126: Indexes: [fkrgq3f3ehhqevn8bet6vua9ekc, primary]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000261: Table found: drogaria.tbl_venda

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000037: Columns: [tbl_funcionario_fun_codigo, ven_horario, ven_valor_total, ven_codigo]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000108: Foreign keys: [fkbm6cs0iafro7arjtct2hlv1b8, fk_1hxl6xntuebqju9wseas9uiwy]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.TableMetadata 

INFO: HHH000126: Indexes: [fkbm6cs0iafro7arjtct2hlv1b8, primary]

jun 02, 2017 7:39:45 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute

INFO: HHH000232: Schema update complete

Testcase: excluir(br.com.drogaria.teste.FabricanteDaoteste):SKIPPED
Testcase: editar(br.com.drogaria.teste.FabricanteDaoteste):SKIPPED
Testcase: listar(br.com.drogaria.teste.FabricanteDaoteste):SKIPPED
Testcase: salvar(br.com.drogaria.teste.FabricanteDaoteste):SKIPPED
test:
Deleting: C:\Users\THIAGO~1\AppData\Local\Temp\TEST-br.com.drogaria.teste.FabricanteDaoteste.xml
CONSTRUÍDO COM SUCESSO (tempo total: 20 segundos)

Criado 2 de junho de 2017
Respostas 0
Participantes 1