Question

I'm using JSF 2.0, Primefaces and Omnifaces in a little project but I'm having some troubles (or bugs, I really don't know).

The problem is:
I inserted a class and set a course for this class (Class: "Course1", Course:"Gambiarra"): enter image description here

When I try to edit the same class, everything in the CRUD is okay but the default value of Course is wrong. As you can wait, the correct default value for course is "Gambiarra", but this shows me "Técnico em Informática": enter image description here

I want solve this problem, because user expect the default course in this field, not just the first on the list. Following is my code.

View - JSF:

<h:panelGrid styleClass="col-lg-10 center">
    <label for="curso">Curso:</label>
    <p:selectOneMenu id="curso" value="#{turmaMBean.selecionado.curso}" converter="omnifaces.SelectItemsConverter" style="margin-bottom: 15px;">
        <f:selectItems value="#{turmaMBean.listaTodosCursos}" var="curso" itemLabel="#{curso.nome}" itemValue="#{curso}"/>
    </p:selectOneMenu>
</h:panelGrid>

TurmaMBean:

(note that "Turma selecionado" is setted when user click in a row of dataTable - the set is okay, in tests selecionado.getCurso() gives me the right Curso)

private ArrayList<Curso> listaTodosCursos; // list of all courses
private Turma selecionado;
private ControleCurso controleCurso;       // courseControl 

public TurmaMBean() {

    if (controleCurso == null) {
        controleCurso = ControleCurso.getInstance();
    }

    if (listaTodosCursos == null){
        listaTodosCursos = controleCurso.consulta();
    }

}

// Getters and Setters
public ControleCurso getControleCurso() {
    return controleCurso;
}

public void setControleCurso(ControleCurso controleCurso) {
    this.controleCurso = controleCurso;
}

public Turma getSelecionado() {
    return selecionado;
}

public void setSelecionado(Turma selecionado) {
    this.selecionado = selecionado;
}

public ArrayList<Curso> getListaTodosCursos() {
    return listaTodosCursos;
}

public void setListaTodosCursos(ArrayList<Curso> listaTodosCursos) {
    this.listaTodosCursos = listaTodosCursos;
}

Turma:

public class Turma {

    private int id;
    private String nome;
    private Curso curso;

    public Turma() {

    }

    public Turma(int id, String nome){
        this.id = id;
        this.nome = nome;
    }

    public Turma(int id, String nome, Curso idCurso) {
        this.id = id;
        this.nome = nome;
        this.curso = idCurso;
    }

    public Turma(String nome, Curso idCurso) {
        this.nome = nome;
        this.curso = idCurso;
    }

    public int getId() {
        return id;
    }

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

    public String getNome() {
        return nome;
    }

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

    public Curso getCurso() {
        return curso;
    }

    public void setCurso(Curso idCurso) {
        this.curso = idCurso;
    }

    @Override
    public String toString() {
        return nome;
    }  
}

Already tried this answer but I can't understand, so this don't solve my problem.

If you need some other information, please tell me I will provide everything.

Was it helpful?

Solution

I don't have much experience with OmniFaces and its converters, but generally using converters with object class that don't override hashCode() and equalsTo(...) methods can lead to some unexpected results. It is very likely that is what you're experiencing in this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top