Pessoal o meu datatable não está listando os dados do banco de dados.Acredito que o erro seja na minha classe UsuarioDaoImpl .Gostaria de um help aqui no aguardo.
Segue as minhas classes junto com os arquivos de configuração do hibernate.
package model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Usuario generated by hbm2java
*/
@Entity
@Table(name="usuario"
,schema="public"
)
public class Usuario implements java.io.Serializable {
private long codigo;
private String usuario;
private String senha;
public Usuario() {
}
public Usuario(long codigo) {
this.codigo = codigo;
}
public Usuario(long codigo, String usuario, String senha) {
this.codigo = codigo;
this.usuario = usuario;
this.senha = senha;
}
@Id
@Column(name="codigo", unique=true, nullable=false)
public long getCodigo() {
return this.codigo;
}
public void setCodigo(long codigo) {
this.codigo = codigo;
}
@Column(name="usuario", length=40)
public String getUsuario() {
return this.usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
@Column(name="senha", length=8)
public String getSenha() {
return this.senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
}
UsuarioDao
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import java.util.List;
import model.Usuario;
/**
*
* @author Administrador
*/
public interface UsuarioDao {
public Usuario findByUsuario(Usuario usuario);
public Usuario login(Usuario usuario);
public List<Usuario> findAll();
}
UsuarioDaoImpl
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import java.util.List;
import model.Usuario;
import org.hibernate.Session;
import util.HibernateUtil;
/**
*
* @author Administrador
*/
public class UsuarioDaoImpl implements UsuarioDao {
@Override
public Usuario findByUsuario(Usuario usuario) {
Usuario model = null;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
String sql="FROM Usuario WHERE usuario = '"+usuario.getUsuario()+"'";
try {
session.beginTransaction();
model = (Usuario) session.createQuery(sql).uniqueResult();
session.beginTransaction().commit();
}catch (Exception e ) {
// session.beginTransaction().rollback();
session.close();
}
return model;
}
@Override
public Usuario login(Usuario usuario) {
Usuario model = this.findByUsuario(usuario);
if(model != null) {
if(!usuario.getSenha().equals(model.getSenha())) {
model = null;
}
}
return model;
}
@Override
public List<Usuario> findAll() {
List <Usuario> listado = null;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
String sql="FROM Usuario";
try {
session.beginTransaction();
listado = session.createCriteria(sql).list();
session.beginTransaction().commit();
}catch (Exception e ) {
// session.beginTransaction().rollback();
session.close();
}
return listado;
}
}
UsuarioBean
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bean;
import dao.UsuarioDao;
import dao.UsuarioDaoImpl;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import model.Usuario;
/**
*
* @author Administrador
*/
@Named(value = "usuarioBean")
@RequestScoped
public class usuarioBean {
private List<Usuario> usuarios;
private Usuario selectedUsuario;
public usuarioBean() {
this.usuarios = new ArrayList<Usuario>();
}
public List<Usuario> getUsuarios() {
UsuarioDao usuarioDao = new UsuarioDaoImpl();
this.usuarios = usuarioDao.findAll();
return usuarios;
}
/**
* @return the selectedUsuario
*/
public Usuario getSelectedUsuario() {
return selectedUsuario;
}
public void setSelectedUsuario(Usuario selectedUsuario) {
this.selectedUsuario = selectedUsuario;
}
}
Configuração do Hibernate
<?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.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/dados</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">postgres</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
<mapping resource="/Usuario.hbm.xml"/>
<mapping resource="/Paciente.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate.reveng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection match-catalog="dados" match-schema="public"/>
<table-filter match-name="usuario"/>
<table-filter match-name="paciente"/>
</hibernate-reverse-engineering>
Usuario.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 15/11/2014 23:21:09 by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="model.Usuario" table="usuario" schema="public" optimistic-lock="version">
<id name="codigo" type="long">
<column name="codigo" />
<generator class="assigned" />
</id>
<property name="usuario" type="string">
<column name="usuario" length="40" />
</property>
<property name="senha" type="string">
<column name="senha" length="8" />
</property>
</class>
</hibernate-mapping>
HibernateUtil
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package util;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
/**
* Hibernate Utility class with a convenient method to get Session Factory
* object.
*
* @author Administrador
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
MyUtil
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package util;
/**
*
* @author Administrador
*/
public class myUtil {
public static String baseurl()
{
return "http://localhost:8080/BioMed/";
}
public static String baseparalogin()
{
return "/BioMed/faces/views/inicio.xhtml";
}
public static String baseparausuario()
{
return "/faces/views/";
}
}
Meu dataTable Primefaces
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<body>
<ui:composition template="./../template.xhtml">
<ui:define name="content">
<p:growl id="msgs" showDetail="true" />
<h:form id="formDataTable">
<p:dataTable id="basicDT" var="usuario" value="#{usuarioBean.usuarios}">
<f:facet name="header">
Basic
</f:facet>
<p:column headerText="Codigo">
<h:outputText value="#{usuario.codigo}" />
</p:column>
<p:column headerText="Usuario">
<h:outputText value="#{usuario.usuario}" />
</p:column>
<p:column headerText="Senha">
<h:outputText value="#{usuario.senha}" />
</p:column>
<p:column style="width:32px;text-align: center">
<p:commandButton update=":formUpdate" oncomplete="dialogUsuario.show()" icon="ui-icon-search" title="View">
<f:setPropertyActionListener value="#{usuario}" target="#{usuarioBean.selectedUsuario}" />
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<h:form id="formUpadte">
<p:dialog id="dlgUsuario" header="Alterar Usuario" widgetVar="dialogUsuario" modal="true" showEffect="fade" hideEffect="fade" resizable="false">
<p:outputPanel id="carDetail" style="text-align:center;">
<p:panelGrid id="display" columns="2" columnClasses="label,value">
<h:outputText value="Codigo" />
<h:outputText value="#{usuarioBean.selectedUsuario.codigo}" />
<h:outputText value="Usuario" />
<h:outputText value="#{usuarioBean.selectedUsuario.usuario}" />
<h:outputText value="Senha" />
<h:outputText value="#{usuarioBean.selectedUsuario.senha}"/>
</p:panelGrid>
</p:outputPanel>
</p:dialog>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
