Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Dúvida no Ex. 5 da Aula 7

esta sendo apresentada a seguinte mensagem de erro "javax.servlet.ServletException: /sorteio.xhtml @23,48 value="#{par.sorteio.nome}": The class 'java.lang.String' does not have the property 'sorteio'."

porem comparei as minhas alterações com a resposta de exercício e esta bem similar... segue abaixo

SorteioBean:

@Named
@RequestScoped
public class SorteioBean {

    private Sorteio sorteio = new Sorteio();

    @Inject
    private ParticipanteDao participanteDao;

    @Inject
    private SorteioDao sorteioDao;

    public Sorteio getSorteio() {
        return sorteio;
    }

    public void sortear() {
        List<Participante> participantes = participanteDao.getParticipantes();

        try {
            Sorteador sorteador = new Sorteador(sorteio, participantes);
            sorteador.sortear();
            sorteioDao.inserir(sorteio);
        } catch (SorteioException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public List<Par> getPares() {
        return sorteioDao.getPares();
    }

}

SorteioDao:

@Stateless
public class SorteioDao {

    @PersistenceContext
    private EntityManager em;

    public void inserir(Sorteio sorteio) {
        em.persist(sorteio);
    }

    public List<Par> getPares() {
        return em.createNativeQuery("select * from Par", Par.class).getResultList();
    }

}

sorteio.xhtml:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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:p="http://primefaces.org/ui"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Sorteio</title>
</h:head>
<h:body>
    <h:form>
        <p:panelGrid columns="2">
            <p:outputLabel value="Nome: " />
            <p:inputText value="#{sorteioBean.sorteio.nome}"/>
            <p:commandButton action="#{sorteioBean.sortear}" value="Cadastrar" ajax="false"/>
        </p:panelGrid>
    </h:form>
    <br />
    <p:dataTable value="sorteioBean.pares" var="par">
        <p:column>
            <f:facet name="header">Sorteio:</f:facet>
            <h:outputText value="#{par.sorteio.nome}" />
        </p:column>
        <p:column>
            <f:facet name="header">Amigo:</f:facet>
            <h:outputText value="#{par.amigo.nome}" />
        </p:column>
        <p:column>
            <f:facet name="header">Amigo Oculto:</f:facet>
            <h:outputText value="#{par.amigoOculto.nome}" />
        </p:column>
    </p:dataTable>
</h:body>
</html>
3 respostas

Pela mensagem de erro o problema está na sua classe Par, que procura uma propriedade sorteio como string. Provavelmente no datatable. Pode colocar o código dela aí ?

Segue abaixo

Par:

@Entity
public class Par {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @ManyToOne
    private Participante amigo;

    @ManyToOne
    private Participante amigoOculto;

    @ManyToOne
    private Sorteio sorteio;

    public Par(Participante amigo, Participante amigoOculto, Sorteio sorteio) {
        this.amigo = amigo;
        this.amigoOculto = amigoOculto;
        this.sorteio = sorteio;
    }

    Par() {
    }

    public Integer getId() {
        return id;
    }

    public Participante getAmigo() {
        return amigo;
    }

    public Participante getAmigoOculto() {
        return amigoOculto;
    }

    public Sorteio getSorteio() {
        return sorteio;
    }

}

Sorteio:

@Entity
public class Sorteio {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String nome;

    @OneToMany(mappedBy = "sorteio", cascade = CascadeType.PERSIST)
    private Set<Par> pares = new LinkedHashSet<>();

    public Integer getId() {
        return id;
    }

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

    public String getNome() {
        return nome;
    }

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

    public Set<Par> getPares() {
        return Collections.unmodifiableSet(pares);
    }

    public void setPares(Set<Par> pares) {
        this.pares = pares;
    }

    public void addPar(Par par) {
        this.pares.add(par);
    }

    public int getQuantidadeDePares() {
        return pares.size();
    }

}
solução!

descobri o que era, faltou colocar o sorteioBean entre #{ } no xhtml

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software