Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrimeFaces 3.0.M3 Cell Editor does not update value

I have read there, but i can't take edited value from primefaces datatable cellEditor, it gives me unedited value. i am using jpa. xhtml page:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE html>
    <ui:composition xmlns="http://www.w3.org/1999/xhtml"                    
                    xmlns:ui="http://java.sun.com/jsf/facelets"
                    xmlns:h="http://java.sun.com/jsf/html"
                    xmlns:f="http://java.sun.com/jsf/core"
                    xmlns:p="http://primefaces.prime.com.tr/ui"
                    template="/templates/masterLayout.xhtml">
        <ui:define name="windowTitle">
            learn
        </ui:define>
        <ui:define name="content">
            <h:form>
                <p:dataTable value="#{lesson.lessonValue}" var="l" style="width: 400px">
                    <p:ajax event="rowEdit" listener="#{lesson.onEditRow}"/>
                    <p:column headerText="Lessons" style="width: 300px">
                        <p:cellEditor>
                            <f:facet name="output">
                                <h:outputText value="#{l.lessonName}"/>
                            </f:facet>
                            <f:facet name="input">
                                <p:inputText value="#{l.lessonName}" style="width: 100%"/>
                            </f:facet>
                        </p:cellEditor>
                    </p:column>
                    <p:column headerText="Options">
                        <p:rowEditor />
                    </p:column>
                </p:dataTable>
            </h:form>
        </ui:define>
    </ui:composition> 

lesson.java:

public class lesson implements Serializable {
    private String name;
    protected EntityLesson[] lessonList;

    public String getName() { return name; }
    public void setName(String newValue) { name = newValue; }

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("DefaultPU");
        public EntityLesson[] getLessonValue() {
        EntityManager em = emf.createEntityManager();
        List<EntityLesson> result;
        try {
            EntityTransaction entr = em.getTransaction();
            boolean committed = false;
            entr.begin();
            try {
                Query query = em.createQuery("SELECT l FROM EntityLesson l");
                result = query.getResultList();
                entr.commit();
                committed = true;
                lessonList = new EntityLesson[result.size()];
                lessonList = result.toArray(lessonList);
            } finally {
                if (!committed) entr.rollback();
            }
        } finally {
            em.close();
        }
        return lessonList;
    }
    public void onEditRow(RowEditEvent event) {
        EntityLesson editedLesson = (EntityLesson)event.getObject();//gives me unedited value
        ............................
        ............................
    }

EntityLesson.java:

@Entity
@Table(name="lessonaaa")

public class EntityLesson implements Serializable {
    @Id
    @Column(name="Lesson_Id", nullable=false)
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int lessonId;

    @Column(name="Lessson", nullable=false, length=65)
    private String lessonName;

    public int getLessonId() { return lessonId; }
    public void setLessonId(int lessonId) { this.lessonId = lessonId; }

    public String getLessonName() { return lessonName; }
    public void setLesson (String lessonName) { this.lessonName = lessonName; }
    }
like image 287
Deniz Avatar asked Jan 19 '23 13:01

Deniz


1 Answers

The problem occurs because of the JSF lifecycle:

When your dataTable is displayed it executes the JPQL to retrieve the list of lessons. After that they are displayed. Now you edit on entity and hit save, the edited entity in the list has now the new value. But what happens next, is that the list is fetched another time and then the listener method is executed with the newly fetched enitiy.

You can solve the problem if you store the list of entities in a local attribute in the view bean and fill it in the post construct method (annotated by @PostContruct) and you have to make the view bean @SessionScoped. Then use this list for the datatable.

like image 195
McIntosh Avatar answered Jan 27 '23 21:01

McIntosh