How do UISelectOne and UISelectMany components preselect defaults in f:selectItems
It uses Object#equals()
for that. You can change (fix) this behavior by implementing it accordingly on your entity.
private Long id;
@Override
public boolean equals(Object other) {
return (other != null && getClass() == other.getClass() && id != null)
? id.equals(getClass().cast(other).id)
: (other == this);
}
Don't forget the hashCode()
to satisfy the equals-hashCode contract.
@Override
public int hashCode() {
return (id != null)
? (getClass().hashCode() + id.hashCode())
: super.hashCode();
}
If you can't change the existing entity for some unclear reason, wrap it in your own DTO.
The converter only converts between the entity and its unique String
representation for usage in HTML output and HTTP request parameters and has therefore no influence on preselection. It has only influence on potential Validation Error: Value is not valid trouble.
See also:
- How to populate options of h:selectOneMenu from database?