3

I'm using selectonemenu like this:

<h:selectOneMenu value="#{MyBean.zajecie.przedmiot}">
    <f:selectItems value="#{MyBean.przedmioty}" var="p"
        itemLabel="#{p.nazwa}" itemValue="#{p}" />
    <f:converter converterId="converter.PrzedmiotConverter" />
</h:selectOneMenu>

MyBean:

private Zajecie zajecie;//+set get
private List<Przedmiot> przedmioty;//+set get

@PostConstruct
private void init() {
    przedmioty = przedmiotDao.findByLogin("login");
    zajecie = new Zajecie();
}

and the converter methods:

public Object getAsObject(FacesContext context, UIComponent component, String value) {
    PrzedmiotDao przedmiotDao = DaoFactory.getInstance().getPrzedmiotDao();
    Przedmiot przedmiot = przedmiotDao.findById(Przedmiot.class, Integer.parseInt(value));
    return przedmiot;
}

public String getAsString(FacesContext context, UIComponent component, Object value) {
    Przedmiot przedmiot = (Przedmiot) value;
    String idAsString = String.valueOf(przedmiot.getPrzedmiotId());
    return idAsString;
}

The selectonemenu component is being populated as it's supposed to. When I submit, it shows Validation Error: Value is not valid. I know i need a proper equals() method for my entities so I've generated it with eclipse using only the id field. Then i had to change the test getClass() != obj.getClass() to obj instanceof Przedmiot because obj.getClass() returned something like this: Przedmiot_$$_javassist_1. I'm not sure if that is relevant because after all obj proves to be null. What am I doing wrong?

Edit:

MyBean is ViewScoped.

Funny thing is that similar code using the same converter works in an other part of the application. The difference is that in the working part I'm just viewing the list of type Przedmiot and I'm obtaining it in another way.

@PostConstruct
private void init() {
    student = studentDao.findByLogin(ra.getUser());
}

<h:selectOneMenu value="#{otherBean.przedmiot}">
    <f:selectItems value="#{otherBean.student.grupa.przedmiots}" var="p" 
        itemLabel="#{p.nazwa}" itemValue="#{p}" />
    <f:converter converterId="converter.PrzedmiotConverter" />
</h:selectOneMenu>
user2270884
  • 91
  • 1
  • 1
  • 7

2 Answers2

6

Solved it. It was of course badly written equals() method. First of all there was a mistake in my question. obj didn't resolve to null but other.przedmiotId did. Sorry for that. Look at the method generated by eclipse:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof Przedmiot))//changed this from (getClass() != obj.getClass())
        return false;
    Przedmiot other = (Przedmiot) obj;
    if (przedmiotId == null) {
        if (other.przedmiotId != null)
            return false;
    } else if (!przedmiotId.equals(other.przedmiotId))
        return false;
    return true;
}

The issue is in other.przedmiotId. When obtaining the value with a getter other.getPrzedmiotId() it doesn't resolve to null anymore.

user2270884
  • 91
  • 1
  • 1
  • 7
1

In your converter: Integer.parseInt(value), and in <f:selectItems you set itemValue="#{p}", so each #{p} is instance of Przedmiot type.

See also: Why selectOneMenu Send ItemLabel to the converter?

Community
  • 1
  • 1
Rong Nguyen
  • 4,143
  • 5
  • 27
  • 53