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