Validation Error: Value is not valid
869637Jun 17 2011 — edited Jun 20 2011Hello everybody! I am new both to JSF and JPA and hope you can help me with a problem I couldn't solve even after several hours of debugging and google searches..
I have 2 entities which are in a ManyToMany relationship:
@Entity
public class TestRun implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToMany(mappedBy="associatedWithTestRuns")
private Collection<Tracker> associatedTrackers;
// getters and setters
@Override
public boolean equals(Object object) {
if (!(object instanceof Tracker)) {
return false;
}
Tracker other = (Tracker) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
}
@Entity
public class Tracker implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToMany
private Collection<TestRun> associatedWithTestRuns;
// getters and setters
//id based equals(), etc
}
They model the fact that a TestRun can be associated with several trackers (and viceversa).
I then have 2 Managed Beans (generated by NetBeans 7.0) called respectively TestRunController and TrackerController: each such bean has an aggregation to an Entity. I show here the relevant code for just one of them, as they are almost identical:
@ManagedBean(name = "trackerController")
@SessionScoped
public class TrackerController implements Serializable {
private Tracker current;
@EJB
private my.TrackerFacade ejbFacade;
public Tracker getSelected() {
if (current == null) {
current = new Tracker();
}
return current;
}
public SelectItem[] getItemsAvailableSelectMany() {
return JsfUtil.getSelectItems(ejbFacade.findAll(), false); // Returns an array of SelectItem(Object value => Tracker, String description)
}
@FacesConverter(forClass = Tracker.class)
public static class TrackerControllerConverter implements Converter {
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
TrackerController controller = (TrackerController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "trackerController");
return controller.ejbFacade.find(getKey(value));
}
java.lang.Long getKey(String value) {
java.lang.Long key;
key = Long.valueOf(value);
return key;
}
String getStringKey(java.lang.Long value) {
StringBuffer sb = new StringBuffer();
sb.append(value);
return sb.toString();
}
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Tracker) {
Tracker o = (Tracker) object;
return getStringKey(o.getId());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + TrackerController.class.getName());
}
}
}
I added into the DB a couple of Trackers and finally created a Facelet for the insertion of a TestRun. This facelet uses a selectManyCheckbox which should allow to choose which trackers are associated with a testrun:
<h:outputLabel value="Tracker" for="tracker" />
<h:selectManyCheckbox id="tracker" value="#{testRunController.selected.associatedTrackers}" title="Tracker">
<f:selectItems value="#{trackerController.itemsAvailableSelectMany}"/>
</h:selectManyCheckbox>
The form gets properly created and displayed and I am even able to create a TestRun record when I don't select any Tracker. But as soon as I select a Tracker, I get the error "Validation Error: Value is not valid" (I think this should correspond to javax.faces.component.UISelectMany.INVALID).
I read in internet (and in this forum as well) that this problem is sometimes connected with a missing overriding of equals(), but this should not be my case... Furthermore, I noticed (while debugging) that such method gets never invoked after the form is sent. The same happens with TrackerControllerConverter#getAsObject(), which according to several posts should be another cause for the problem...
I am relly confused and hope you can help me..
Bye and... THANKS!