I have an editable DataTable for the entity User. This table has a field called username which has an unique constraint
<h:form id="form">
<p:dataTable id="userTable" value="#{users}" var="entity" editable="true" editMode="cell" tableStyle="width:auto">
<p:ajax event="cellEdit" listener="#{userController.onCellEdit}" update="form:msgs form:display form:userTable" />
<p:column headerText="UserID">
<p:outputLabel value="#{entity.id}" />
</p:column>
<p:column headerText="Username">
<p:cellEditor>
<f:facet name="output"><p:outputLabel for="username" value="#{entity.username}" /></f:facet>
<f:facet name="input">
<p:inputText id="username" value="#{entity.username}" >
<f:validateRequired />
<f:validateLength maximum="50" />
</p:inputText>
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</h:form>
In the backing bean userController, I have the following method onCellEdit
public void onCellEdit(CellEditEvent event) {
Object newValue = event.getNewValue();
String columnName = event.getColumn().getHeaderText();
facesContext = FacesContext.getCurrentInstance();
User entity = facesContext.getApplication().evaluateExpressionGet(facesContext, "#{entity}", User.class);
if(columnName.equals("Username") {
entity.setUsername((String) newValue);
}
try {
service.createOrUpdateEntity(entity);
}
catch (EJBTransactionRolledbackException ex) {
Throwable t = ex.getCause();
while (t != null) {
t = t.getCause();
if(t.getClass().getSimpleName().equals("SQLIntegrityConstraintViolationException")) {
entity.setUsername((String) event.getOldValue());
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Duplicate entry for the field username. Write a different username", ""));
break;
}
}
return;
}
catch (Exception e) {
e.printStackTrace();
}
The problem is that when the exception is catched the final value for the facet input is different from the value from the facet output. In this case the facet output shows the right value while the facet input shows the value that produced the exception.