1

Primefaces 5.3 glassfish 4.1

following is my entity manages

@Stateless
public class ManagerTask {

@PersistenceContext(unitName="task")
private EntityManager em;

ManagerTask(){} 

public List<Task> findAllTask() {

    TypedQuery<Task> q =  em.createNamedQuery("Task.findAll",Task.class);
    List<Task> taskList = q.getResultList();

    return taskList;

}   

Now in my CDI bean i want to invoke the findAllTask() method get the list of taks and then in my jsf use it to create a datatable

Method 1:

CDI Bean

@Named(value="bt") 
@ViewScoped 
public class BeanTask {
@SuppressWarnings("unused")
private static final long serialVersionUID = 1L; 


private ManagerTask mt;

private List<Task> tl;
private List<Task> filteredtl;
private ArrayList<SelectItem> regions;

public BeanTask() {
    // TODO Auto-generated constructor stub

}

@Inject
public BeanTask(ManagerTask mt) {
    this.mt=mt;
    tl=mt.findAllTask();
}

jsf

<p:dataTable id="tb1" var="tk" value="#{bt.tl}"
            rowIndexVar="rowindex" styleClass="fixed-size" 
            filteredValue="#{bt.filteredtl}"
            >

This is the method i used to use but i realized that the constructor is called multiple times leading to the execution of multiple repeated queries and as suggested by a member here in a different question that no business logic should go into the constructor of a cdi bean i moved to method 2

Method 2:

CDI Bean

@Inject
private ManagerTask mt;

private List<Task> tl;
private List<Task> filteredtl;
private ArrayList<SelectItem> regions;

public BeanTask() {
    // TODO Auto-generated constructor stub

}


public List<Task> getalltasks() {
    tl=mt.findAllTask();  




    return tl; 
}

jsf

<p:dataTable id="tb1" var="tk" value="#{bt.getalltasks()}"
            rowIndexVar="rowindex" styleClass="fixed-size" 
            filteredValue="#{bt.filteredtl}"
            >

i think the second method might be the correct way to do it but the problem i am having is if i try to sort a column of the datatable it does not work with the second method, i found out from other questions here is because the method is querying the database everytime instead of using a stored value linked here. with method 1 sort works.

is there a completely different way i should be doing this or am i missing something

Community
  • 1
  • 1
yahh
  • 1,175
  • 3
  • 14
  • 41
  • What is the version / type of jsf you are using? Can you tell us from your import statement on BeanTask.java if you have import javax.faces.bean.ViewScoped; or import javax.faces.view.ViewScoped; please? Check out if http://stackoverflow.com/questions/12229561/jsf2-primefaces-datatable-sort-not-working-with-viewscoped-bean and http://balusc.omnifaces.org/2010/06/benefits-and-pitfalls-of-viewscoped.html if it is applicable – Mahendran Ayyarsamy Kandiar Nov 20 '15 at 18:36

1 Answers1

0

I couldn't have a complete idea by the example code you've posted but even so I've put an example together for you that uses column filtering.

Service

@Stateless
public class TaskManager {

    @PersistenceContext(unitName = "task")
    private EntityManager em;

    public List<Task> findAll() {
        return em.createNamedQuery("Task.findAll", Task.class).getResultList();
    }   

    public void save(Task task) {
        em.persist(task);
    }
}

Controller

A simple controller with a properly set filter for PrimeFaces's dataTables. Whenever an event occurs that would change the database state, like saving, updating or deleting, just reload the data from database. For this I like to have a separate method, like the reset() method below.

@Named
@ViewScoped 
public class TaskBean {

    private static final long serialVersionUID = 1L; 

    @Inject
    private TaskManager taskManager;

    // Filtered entity list
    private List<Task> filteredTasks;

    // Main entity list
    private List<Task> tasks;

    // Main entity
    private Task task;

    @PostConstruct
    public void init() {
        reset();
    }

    private void reset() {
        tasks = taskManager.findAll();
    }

    public void create() {
         task = new Task();
    }

    public void save() {
        taskManager.save(task);
        reset(); 
    }

    public Task getTask() {
        return task;
    }

    public List<Task> getTasks() {
        return tasks;
    }

    public List<Task> getFilteredTasks() {
         return filteredTasks;
    }   

    public void setFilteredTasks(List<Task> filteredTasks) {
        this.filteredTasks = filteredTasks;
    }   
}

View

A simple form example

Notice that it updates the taskDataTable after a save event.

<h:form id="taskForm" prependId="false">
    <p:inputText id="taskFormName" label="Name" value="#{taskBean.task.name}" />
    <!-- more fields here ... -->
    <p:commandButton id="taskFormSave" actionListener="#{taskBean.save}" value="Save" update="taskDataTable" />
</h:form>

A dataTable with column filtering

<p:dataTable id="taskDataTable" value="#{taskBean.tasks}" filteredValue="#{taskBean.filteredTasks}" var="row">
    <p:column headerText="Name" filterBy="#{row.name}" filterMatchMode="contains">
        <h:outputText value="#{row.name}" />
    </p:column>
    <!-- more columns here ... -->
</p:dataTable>
rbento
  • 9,919
  • 3
  • 61
  • 61