I want to set a many-to-one relationship in a hibernate entity using selectOneMenu
.
<h:selectOneMenu value="#{taskBean.instance.currency}" class="form-control" id="currencyList" converter="#{currencyConverterBean}">
<f:selectItems value="#{currencyBean.currencyList}" var="currency" itemLabel="#{currency.name}"/>
</h:selectOneMenu>
where #{taskBean.instance}
returns a hibernate entity Task:
@Entity
@Table(name="task")
public class Task {
//...
public Currency currency;
@ManyToOne
@JoinColumn(name = "currencyID")
public Currency getCurrency() {
return currency;
}
//...
}
If one sets value #{taskBean.instance.currency.id}
for selectOneMenu and #{currency.id}"
for itemValue it doesn't work. Therefore, I tryed to use a converter:
@ManagedBean(name="currencyConverterBean")
@FacesConverter(value = "currencyConverter")
@RequestScoped
public class CurrencyConverter implements Converter {
CurrencyBO currencyBO;
@Override
public Currency getAsObject(FacesContext ctx, UIComponent component, String value) {
return getCurrencyBO().getCurrencyById(new Integer(value));
}
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object o) {
return String.valueOf(((Currency) o).getId());
}
public CurrencyBO getCurrencyBO() {
return currencyBO;
}
public void setCurrencyBO(CurrencyBO currencyBO) {
this.currencyBO = currencyBO;
}
}
It doesn't work. It returns the following error:
PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException at com.otv.converter.CurrencyConverter.getAsObject(CurrencyConverter.java:33)
What am I doing wrong?