我有这样的实体,称为“操作”:
@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
}
我检索操作是这样的:(?这可能是通过一个EJB实例,但只是为了保持它的地方和作为一个例子,好不好;))
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;
}
所以我试图让这个选定的操作selectOneMenu
:
该productc
是ManagedBean
具有viewScope
, productb
是具有一个ManagedBean sessionScope
其中有一个product
是我的实体。 该产品contais一个operation
,所以是这样的:
(字母C具有控制权,其中涉及关于我的实体产品的所有操作都应该由这个bean来处理的意思,好吗?)
Product productc (ViewScope)
-- ProductBean productb (SessionScope)
---- Product product (Entity)
-------- Operation operation (Entity)
该转换器是一样的@BalusC是前建议:
@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;
}
其中检索所选择的操作日志中显示:
FINE: SELECT ID, OPERATION FROM operation WHERE (ID = ?)
bind => [1 parameter bound]
INFO: Getting the operation value = exchange
所以,当我尝试提交表单给出了如下错误:
form_add_product:operation: Validation error: the value is not valid
这究竟是为什么?