Boa noite a todos aqui do forum,
1º Problema [Resolvido]
Estou com o seguinte problema, implementei primeiramente um selectOneMenu para o meu campo de setores e funcionou perfeitamente...
Como minha classe fechadura, possui 2 campos FK, gostaria de pegar tanto o Setor como o MAC do dispositivo ZigBee...
Quando utilizo as duas classes... ele pega somente os MAC dos dispositivos ZigBee, deixando a classe setor vazia... Alguem sabe me dizer o porque??
@PostConstruct
public void carregaSelectOneMenu(){
SetorDao setorDAO = new SetorDao();
listaSetor = setorDAO.buscarTodos();
ZigbeeDao zigbeeDAO = new ZigbeeDao();
listaZigbee = zigbeeDAO.buscarTodos();
}
2º Problema
Agora ele não realiza o cadastro quando clico no botão salvar!!!
<?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://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html">
<body>
<ui:composition template="./template.xhtml">
<ui:define name="content">
<h:form id="frmCadastroFechadura">
<p:messages autoUpdate="true"/>
<p:panel header="Cadastro de Fechadura">
<h:panelGrid columns="2" cellpadding="5">
<h:outputText value="ID: "/>
<p:inputText id="id" required="true" requiredMessage="Preenchimento obrigatório" value="#{fechaduraBean.fechadura.idfechadura}"/>
<h:outputText value="Nome: "/>
<p:inputText id="nome" required="true" requiredMessage="Preenchimento obrigatório" value="#{fechaduraBean.fechadura.nome}"/>
<h:outputText value="Setor: "/>
<p:selectOneMenu id="idsetor" value="#{fechaduraBean.fechadura.idsetor}" converter="converterSetor">
<f:selectItem itemLabel="Selecione" />
<f:selectItems value="#{fechaduraBean.listaSetor}" var="set" itemValue="#{set}" itemLabel="#{set.nome}"/>
</p:selectOneMenu>
<h:outputText value="ZigBee: "/>
<p:selectOneMenu id="idzigbee" value="#{fechaduraBean.fechadura.idzigbee}" converter="converterZigbee">
<f:selectItem itemLabel="Selecione" />
<f:selectItems value="#{fechaduraBean.listaZigbee}" var="zig" itemValue="#{zig}" itemLabel="#{zig.mac}" />
</p:selectOneMenu>
<h:outputText value="Status: "/>
<p:selectOneMenu value="#{fechaduraBean.fechadura.status}">
<f:selectItem itemLabel="Selecione" itemValue="" />
<f:selectItem itemLabel="Ativo" itemValue="Ativo" />
<f:selectItem itemLabel="Bloqueado" itemValue="Bloqueado" />
<f:selectItem itemLabel="Desabilitado" itemValue="Desabilitado" />
</p:selectOneMenu>
<f:facet name="footer">
<p:commandButton value="Salvar" update="frmCadastroFechadura,tabela" actionListener="#{fechaduraBean.salvar()}" icon="ui-icon-disk"/>
<p:commandButton value="Limpar" type="reset" />
</f:facet>
</h:panelGrid>
<p:dataTable id="tabela" var="fechadura" value="#{fechaduraBean.listarTodos()}" >
<p:column>
<h:outputText value ="#{fechadura.nome}" />
</p:column>
<p:column>
<h:outputText value ="#{fechadura.idfechadura}" />
</p:column>
<p:column>
<h:outputText value ="#{fechadura.idsetor}" />
</p:column>
</p:dataTable>
</p:panel>
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
package controle.fechadura;
import dao.FechaduraDao;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import modelo.Fechadura;
import modelo.Setor;
import dao.SetorDao;
import dao.ZigbeeDao;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import modelo.Zigbee;
/**
*
* @author Ulisses
*/
@ManagedBean
@ViewScoped
public class FechaduraBean {
private Fechadura fechadura;
private Setor setor;
private Zigbee zigbee;
private List<Fechadura> listaFechadura;
private List<Setor> listaSetor;
private List<Zigbee> listaZigbee;
/**
* Creates a new instance of FechaduraBean
*/
public FechaduraBean() {
fechadura = new Fechadura();
listaFechadura = new ArrayList<Fechadura>();
}
public Fechadura getFechadura() {
return fechadura;
}
public void setFechadura(Fechadura fechadura) {
this.fechadura = fechadura;
}
public List<Fechadura> getListaFechadura() {
return listaFechadura;
}
public void setListaFechadura(List<Fechadura> listaFechadura) {
this.listaFechadura = listaFechadura;
}
public Setor getSetor() {
return setor;
}
public void setSetor(Setor setor) {
this.setor = setor;
}
public List<Setor> getListaSetor() {
return listaSetor;
}
public void setListaSetor(List<Setor> listaSetor) {
this.listaSetor = listaSetor;
}
public Zigbee getZigbee() {
return zigbee;
}
public void setZigbee(Zigbee zigbee) {
this.zigbee = zigbee;
}
public List<Zigbee> getListaZigbee() {
return listaZigbee;
}
public void setListaZigbee(List<Zigbee> listaZigbee) {
this.listaZigbee = listaZigbee;
}
public List<Fechadura> listarTodos(){
FechaduraDao fechaduraDAO = new FechaduraDao();
List<Fechadura> listaFechadura = new ArrayList<Fechadura>();
listaFechadura = fechaduraDAO.buscarTodos();
return listaFechadura;
}
public void salvar(){
FechaduraDao fechaduraDAO = new FechaduraDao();
fechaduraDAO = new FechaduraDao();
fechaduraDAO.inserir(fechadura);
}
@PostConstruct
public void carregaSetor(){
SetorDao setorDAO = new SetorDao();
listaSetor = setorDAO.buscarTodos();
}
@PostConstruct
public void carregaZigbee(){
ZigbeeDao zigbeeDAO = new ZigbeeDao();
listaZigbee = zigbeeDAO.buscarTodos();
}
}
@FacesConverter(value = "converterZigbee")
public class ConverterZigbee implements Converter{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value){
if(value != null && !value.equals("")){
ZigbeeDao dao = new ZigbeeDao();
return dao.buscarPorId(Integer.valueOf(value));
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value){
if (value instanceof Zigbee){
Zigbee zigbee = (Zigbee) value;
return String.valueOf(zigbee.getIdzigbee());
}
return "";
}
}
package controle.setor;
import dao.SetorDao;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import modelo.Setor;
/**
*
* @author Ulisses
*/
@FacesConverter (value = "converterSetor")
public class ConverterSetor implements Converter{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value){
if(value != null && !value.equals("")){
SetorDao dao = new SetorDao();
return dao.buscarPorId(Integer.valueOf(value));
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value){
if (value instanceof Setor){
Setor setor = (Setor) value;
return String.valueOf(setor.getIdsetor());
}
return "";
}
}
package dao;
import java.util.List;
import modelo.Setor;
import org.hibernate.Session;
/**
* @author Ulisses
*/
public class SetorDao extends GenericDao {
private Session ses;
public SetorDao(){
}
public SetorDao(Session ses){
this.ses = ses;
}
public Setor buscarPorId(Integer id) {
if (id == null) {
return null;
} else {
ses = this.getSession();
Setor setor = ( Setor )
ses.get( SetorDao.class, id );
ses.getTransaction().commit();
ses.close();
return setor;
}
}
public List<Setor> buscarTodos(){
ses = this.getSession();
List<Setor> listaSetor = (List<Setor>) ses.createQuery("SELECT a FROM Setor a ORDER BY a.nome").list();
ses.getTransaction().commit();
ses.close();
return listaSetor;
}
public Setor buscarPorNome(String nome){
ses=this.getSession();
Setor setor = (Setor) ses.createQuery("Select p from Setor p where p.nome like ?").setString(0, "%"+nome+"%");
ses.getTransaction().commit();
ses.close();
return setor;
}
}
package dao;
import java.util.List;
import modelo.Zigbee;
import org.hibernate.Session;
/**
*
* @author Ulisses
*/
public class ZigbeeDao extends GenericDao{
private Session ses;
public ZigbeeDao() {
}
public ZigbeeDao(Session ses) {
this.ses = ses;
}
public Zigbee buscarPorId(Integer id) {
if (id == null) {
return null;
} else {
ses = this.getSession();
Zigbee obj = ( Zigbee )
ses.get( SetorDao.class, id );
ses.getTransaction().commit();
ses.close();
return obj;
}
}
public List<Zigbee> buscarTodos(){
ses = this.getSession();
List<Zigbee> listaObj = (List<Zigbee>) ses.createQuery("SELECT a FROM Zigbee a ORDER BY a.mac").list();
ses.getTransaction().commit();
ses.close();
return listaObj;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import util.HibernateUtil;
/**
*
* @author Alunos
*/
public abstract class GenericDao {
//Verificado já okok
protected Session getSession()
{
return HibernateUtil.getinstance().getSession();
}
public GenericDao()
{
}
public String inserir(Object obj){
try
{
Session ses=this.getSession();
//ses.beginTransaction();
ses.save(obj);
ses.getTransaction().commit();
ses.close();
return "Dados inseridos com sucesso!";
}
catch(Exception e)
{
return "Erro";
}
}
public String atualizar(Object obj)
{
Session ses=this.getSession();
ses.flush();
ses.clear();
// ses.beginTransaction();
ses.update(obj);
ses.getTransaction().commit();
ses.close();
return "Dados atualizados com sucesso!";
}
public String remover(Object obj)
{
Session ses = this.getSession();
//ses.beginTransaction();
ses.delete(obj);
ses.getTransaction().commit();
ses.close();
return "Dados removidos com sucesso!";
}
}