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>