I have ad datatable with editable cells. When i click to enter new value to cell, this new value is not showed up. The attached image describes this issue. In the onCellEdit() method, oldValue and newValue have the same value. It is very strange. Below is my jsf and managed bean code.
https://postimg.cc/p9P8tZTy
.xhtml
Code: Select all
<p:dataTable widgetVar="autoidTable" scrollable="true" scrollHeight="450" emptyMessage="" editable="true" editMode="cell"
value="#{autoIdController.findAll(sessionInfoBean.currentDatabase)}" var="autoid"
selection="#{autoIdBean.selectedAutoId}" selectionMode="single" rowKey="#{autoid.refTypeCategoryID}"
draggableColumns="true" resizableColumns="true" resizeMode="expand" rendered="#{optionsBean.index == 6}">
<p:ajax event="cellEdit" listener="#{autoIdBean.onCellEdit}" />
<p:ajax event="rowSelect" listener="#{autoIdBean.rowSelect}"/>
<p:ajax event="rowUnselect" listener="#{autoIdBean.rowUnSelect}"/>
<p:column headerText="Loai chung tu, danh muc" width="200">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{autoid.refTypeCategoryName}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{autoid.refTypeCategoryName}" readonly="true"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Tien to" width="100">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{autoid.prefix}" style="display: block;"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{autoid.prefix}" style="width: 100px"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Gia tri" width="50">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{autoid.value}" style="display: block;"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{autoid.value}" style="width: 50px"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="So ky tu phan so" width="100">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{autoid.lengthOfValue}" style="display: block;"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{autoid.lengthOfValue}" style="width: 100px"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Hau to" width="50">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{autoid.suffix}" style="display: block;"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{autoid.suffix}" style="width: 80px"/>
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
Code: Select all
@Named
@ViewScoped
public class AutoIdBean implements Serializable {
private static final long serialVersionUID = 1L;
@Getter
@Setter
private Autoid selectedAutoId;
@Getter
private List<Autoid> editedRows;
@Getter
private boolean added = false;
@Inject
private DbOptionController dbOptionController;
@Inject
private SessionInfoBean sessionInfoBean;
@PostConstruct
public void init() {
editedRows = new ArrayList<>();
}
public void submit() {
}
public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
System.out.println("oldValue = " + oldValue + "\tnewValue = " + newValue);
if (newValue != null && !newValue.equals(oldValue) && !added) {
editedRows.add(selectedAutoId);
added = true;
}
}
public void rowSelect(SelectEvent event) {
}
public void rowUnSelect(UnselectEvent event) {
}
}
Code: Select all
@Stateless
@Named
public class AutoIdController {
@Inject
private AutoIdDao autoIdDao;
public List<Autoid> findAll(String multitenancyIdentifier) {
return autoIdDao.findAll(multitenancyIdentifier);
}
public Autoid findByRefTypeCategoryID(Integer refTypeCategoryID, String multitenancyIdentifier) {
return autoIdDao.findByRefTypeCategoryID(refTypeCategoryID, multitenancyIdentifier);
}
public Autoid save(Autoid autoid, String multitenancyIdentifier) {
return autoIdDao.save(autoid, multitenancyIdentifier);
}
public Autoid update(Autoid autoid, String multitenancyIdentifier) {
return autoIdDao.update(autoid, multitenancyIdentifier);
}
public void remove(Autoid autoid, String multitenancyIdentifier) {
autoIdDao.remove(autoid, multitenancyIdentifier);
}
}
Code: Select all
AutoIdDao .java
@Stateless
public class AutoIdDao extends Dao<Autoid> {
public List<Autoid> findAll(String multitenancyIdentifier) {
return getEntityManager(multitenancyIdentifier).createNamedQuery("Autoid.findAll", Autoid.class)
.getResultList();
}
public Autoid findByRefTypeCategoryID(Integer refTypeCategoryID, String multitenancyIdentifier) {
return getEntityManager(multitenancyIdentifier).createNamedQuery("Autoid.findAll", Autoid.class).setParameter("refTypeCategoryID", refTypeCategoryID)
.getSingleResult();
}
}
Code: Select all
public abstract class Dao<T> {
@PersistenceUnit
private EntityManagerFactory emf;
protected synchronized EntityManager getEntityManager(String multitenancyIdentifier) {
// Use `unwrap` instead of type casting.
final SessionFactoryImplementor sessionFactory
= emf.unwrap(SessionFactoryImplementor.class);
// Then the rest may be as the following.
final MultitenancyResolver tenantResolver
= (MultitenancyResolver) sessionFactory.
getCurrentTenantIdentifierResolver();
tenantResolver.setTenantIdentifier(multitenancyIdentifier);
return emf.createEntityManager();
}
public T save(T entity, String multitenancyIdentifier) {
EntityManager em = getEntityManager(multitenancyIdentifier);
em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();
return entity;
}
public T update(T entity, String multitenancyIdentifier) {
EntityManager em = getEntityManager(multitenancyIdentifier);
em.getTransaction().begin();
em.merge(entity);
em.getTransaction().commit();
return entity;
}
public void remove(T entity, String multitenancyIdentifier) {
EntityManager em = getEntityManager(multitenancyIdentifier);
em.getTransaction().begin();
em.remove(em.contains(entity) ? entity : em.merge(entity));
em.getTransaction().commit();
}
}