I have this Entity called 'Operation':
@Entity @Table(name="operation") public class Operation implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.SEQUENCE) private Integer id; @NotNull(message="informe um tipo de operação") private String operation; //bi-directional many-to-one association to Product @OneToMany(mappedBy="operation") private List<Product> products; // getter and setters } I retrieve the operations this way: (It could be through an EJB instance but just to keep it local and as an example, okay? ;) )
public Map<String, Object> getOperations() { operations = new LinkedHashMap<String, Object>(); operations.put("Select an operation", new Operation()); operations.put("Donation", new Operation(new Integer(1), "donation")); operations.put("Exchange", new Operation(new Integer(2), "exchange")); return operations; } So I'm trying to get the selected operation in this selectOneMenu:
The productc is a ManagedBean which has a viewScope, productb is a ManagedBean with a sessionScope which has a product which is my entity. The product contais one operation, so is something like this:
(the letter c has the meaning of control, where all operations related about my entity product should be handled by this bean, okay?)
Product productc (ViewScope) -- ProductBean productb (SessionScope) ---- Product product (Entity) -------- Operation operation (Entity) The converter is the same as @BalusC is suggest before:
@ManagedBean @RequestScoped public class OperationConverter implements Converter { @EJB private EaoOperation operationService; @Override public String getAsString(FacesContext context, UIComponent component, Object value) { if (!(value instanceof Operation) || ((Operation) value).getId() == null) { return null; } return String.valueOf(((Operation) value).getId()); } @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { if (value == null || !value.matches("\\d+")) { return null; } Operation operation = operationService.find(Integer.valueOf(value)); System.out.println("Getting the operation value = " + operation.getOperation() ); if (operation == null) { throw new ConverterException(new FacesMessage("Unknown operation ID: " + value)); } return operation; } Which retrieve the operation selected as showed in the log:
FINE: SELECT ID, OPERATION FROM operation WHERE (ID = ?) bind => [1 parameter bound] INFO: Getting the operation value = exchange So when I try to submit the form gives the follow error:
form_add_product:operation: Validation error: the value is not valid Why is this happening?