The problem: if you want to avoid overengineering with DTO classes and use the entities in jsf, then you can get a problem with select boxes and transformation for @OneToMany and @ManyToOne relationships.
The Error message is something like : "' conversion error ... null Converter"
Example (with Java EE 7 and JSF 2.2):
1. Class Game:
public class Game {
@ManyToOne
private Team team1;
...
}
2. Class Team:
public class Team {
private String name;
...
}
3. createGame.xhtml with select box for choosing a first Team:
<h:selectOneListbox id="team1" value="#{gamesBean.createdGame.team1}" converter="#{teamsConverter}">
<f:selectItems value="#{teamsBean.teams}" var="t" itemLabel="#{t.name}" itemValue="#{t}" />
</h:selectOneListbox>
4. TeamsConverter with implemented two methods from Converter interface:
import javax.faces.convert.Converter;
@ManagedBean(name = "teamsConverter")
@FacesConverter(forClass = Game.class)
public class TeamsConverter implements Converter, Serializable {
@Inject
private TeamService teamService;
public TeamsConverter() {
}
@Override
public Object getAsObject(FacesContext context, UIComponent component,
String value) {
if (value.isEmpty()) {
return null;
}
Long id = new Long(value);
return teamService.findById(id);
}
@Override
public String getAsString(FacesContext context, UIComponent component,
Object value) {
if (value == null || value.toString().isEmpty()) {
return "";
}
Team t = (Team) value;
Long id = t.getId();
return id.toString();
}
}
5. The Class Team should overwride hashCode, equals and toString
@Override
public String toString() {
return "de.lustin.flc.domain.Team[ id=" + id + " ]";
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Team)) {
return false;
}
Team other = (Team) object;
if ((this.id == null && other.id != null)
|| (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
Yout can download the complete example from github: