By default, the SelectItemsConverter
relies on toString()
of the entity to match the selected items. Your entity however doesn't have a toString()
implemented and is thus relying on default fqn@hashcode
result which is not the same when two physically different Disciplina
instances are created even though they have the same value.
You've basically 2 options, also hinted in SelectItemsConverter
showcase and javadoc:
Implement a
toString
method that uniquely identifies the entity and which makes sense as an identifier. For example,@Override public String toString() { return String.format("%s[id=%d]", getClass().getSimpleName(), getId()); }
(note that this
toString()
is designed such that you can easily keep it in an abstract base class of all your entities, so that you don't need to copypaste the same over all your entities)Or, if implementing such a
toString()
is not an option for some reason (e.g. relying on generated classes which can't be modified afterwards (neither the generator template)), then extend the converter as follows:@FacesConverter("disciplinaSelectItemsConverter") public class DisciplinaSelectItemsConverter extends SelectItemsConverter { @Override public String getAsString(FacesContext context, UIComponent component, Object value) { Integer id = (value instanceof Disciplina) ? ((Disciplina) value).getId() : null; return (id != null) ? String.valueOf(id) : null; } }
(note: you should really be using
Integer
instead ofint
as ID, theint
cannot benull
which is the correct way to represent a brand new and unpersisted entity)And use it as follows
<h:selectManyCheckbox ... converter="disciplinaSelectItemsConverter">