[Resolvido]Hibernate JSF

Pessoal boa tarde estou querendo utilizar a list do hibernate para fazer um select dos dados do banco e colocar em um dataTable mas está ocorrendo um erro na query do select.

Metodo na classe ReembolsoMB

identar  public List <TbReembolso> verReembolso(){
    Query query= query("select * TbReembolso");
 List<TbReembolso> lista= query.list();
 return lista;
 }

MEtodos do TbReembolsoJpaController

identar public TbReembolsoJpaController(UserTransaction utx, EntityManagerFactory emf) {
    this.utx = utx;
    this.emf = emf;
}
private UserTransaction utx = null;
private EntityManagerFactory emf = null;

public EntityManager getEntityManager() {
    return emf.createEntityManager();
}

public void create(TbReembolso tbReembolso) throws PreexistingEntityException, RollbackFailureException, Exception {
    EntityManager em = null;
    try {
        utx.begin();
        em = getEntityManager();
        em.persist(tbReembolso);
        utx.commit();
    } catch (Exception ex) {
        try {
            utx.rollback();
        } catch (Exception re) {
            throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
        }
        if (findTbReembolso(tbReembolso.getReId()) != null) {
            throw new PreexistingEntityException("TbReembolso " + tbReembolso + " already exists.", ex);
        }
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

public void edit(TbReembolso tbReembolso) throws NonexistentEntityException, RollbackFailureException, Exception {
    EntityManager em = null;
    try {
        utx.begin();
        em = getEntityManager();
        tbReembolso = em.merge(tbReembolso);
        utx.commit();
    } catch (Exception ex) {
        try {
            utx.rollback();
        } catch (Exception re) {
            throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
        }
        String msg = ex.getLocalizedMessage();
        if (msg == null || msg.length() == 0) {
            Integer id = tbReembolso.getReId();
            if (findTbReembolso(id) == null) {
                throw new NonexistentEntityException("The tbReembolso with id " + id + " no longer exists.");
            }
        }
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
    EntityManager em = null;
    try {
        utx.begin();
        em = getEntityManager();
        TbReembolso tbReembolso;
        try {
            tbReembolso = em.getReference(TbReembolso.class, id);
            tbReembolso.getReId();
        } catch (EntityNotFoundException enfe) {
            throw new NonexistentEntityException("The tbReembolso with id " + id + " no longer exists.", enfe);
        }
        em.remove(tbReembolso);
        utx.commit();
    } catch (Exception ex) {
        try {
            utx.rollback();
        } catch (Exception re) {
            throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
        }
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

public List<TbReembolso> findTbReembolsoEntities() {
    return findTbReembolsoEntities(true, -1, -1);
}

public List<TbReembolso> findTbReembolsoEntities(int maxResults, int firstResult) {
    return findTbReembolsoEntities(false, maxResults, firstResult);
}

private List<TbReembolso> findTbReembolsoEntities(boolean all, int maxResults, int firstResult) {
    EntityManager em = getEntityManager();
    try {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(TbReembolso.class));
        Query q = em.createQuery(cq);
        if (!all) {
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
        }
        return q.getResultList();
    } finally {
        em.close();
    }
}

public TbReembolso findTbReembolso(Integer id) {
    EntityManager em = getEntityManager();
    try {
        return em.find(TbReembolso.class, id);
    } finally {
        em.close();
    }
}

public int getTbReembolsoCount() {
    EntityManager em = getEntityManager();
    try {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        Root<TbReembolso> rt = cq.from(TbReembolso.class);
        cq.select(em.getCriteriaBuilder().count(rt));
        Query q = em.createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    } finally {
        em.close();
    }
}

A Query deveria ser

“from TbReembolso” se for JPQL
ou
select * from <tabela_no_banco_do_tbreembolso> se for native query

Você precisa decidir se quer utilizar os recursos do hibernate ou os do jpa.
Se você vai utilizar EntityManager, a query não pode ser a do hibernate, precisa ser do JPA.
Embora possuam o mesmo nome, elas são diferentes.

obrigado por responder, man então como ficaria a query coms erviços do jpa ?

obrigado por responder, man então como ficaria a query coms erviços do jpa ?

Query query = em.createQuery("SELECT c FROM Country c");
List results = query.getResultList();

o em seria o que ?

A mesma coisa que você utiliza aqui.
Das duas uma: ou você fez esse código há muito tempo e não lembra ou foi um baita ctrl + c/ctrl + v…

ah é mesmo rs.

Ma ele apresentou um erro.

Incompatible types javax.persistence cann not be converted to org.hibernate.Query

identar public List <TbReembolso> verReembolso(){
      EntityManager em = null;
     Query query = em.createQuery("SELECT re FROM TbReembolso re");

List results = query.list();

return results;
 
 }

Sim, pois você ainda está instanciando a Query do hibernate e não a do JPA…

era isso mesmo vlw.

Man outra dúvida como faço pra removar o dado, pois no metodo destroy ele está como paramentro um integer e não objeto.

como faço pra passar um inteiro como paramentro.

Metodo TbReembolsoJpaController

identar public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
    EntityManager em = null;
    try {
        utx.begin();
        em = getEntityManager();
        TbReembolso tbReembolso;
        try {
            tbReembolso = em.getReference(TbReembolso.class, id);
            tbReembolso.getReId();
        } catch (EntityNotFoundException enfe) {
            throw new NonexistentEntityException("The tbReembolso with id " + id + " no longer exists.", enfe);
        }
        em.remove(tbReembolso);
        utx.commit();
    } catch (Exception ex) {
        try {
            utx.rollback();
        } catch (Exception re) {
            throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
        }
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

Metodo Excluir magnedBean

identar  public void exlucir() throws RollbackFailureException, Exception {
	System.out.println("Excluir reembolso");
	re.destroy();//passar como paramentro um inteiro ?
	
	FacesContext.getCurrentInstance().addMessage(
			null,
			new FacesMessage(FacesMessage.SEVERITY_INFO,
					"Manutenção de Reembolso: ",
					"Reembolso Excluido!"));
}

Primeiro você precisa recuperar a instância de objeto que representa aquele registro na base de dados (eu ouvi select?). Depois, você pega esse objeto e invoca o método remove.

Pergunta: voce entende o que o código faz? Sabe SQL?

Recomendo dar uma estudada antes de sair copiando código e tentando executar.

blz