Hi all,
I have a question regarding the request bean lifecylce in the current use case (using Sun JSF 1.2)
I have a managed bean in request scope that contains an ArrayList which is used as the data provider in a dataTable on a faces page.
The bean contains an
init() method to populate the ArrayList using a database call.
The dataTable also contains a column with a commandLink that calls a method via
actionListener inside the managed bean to delete the current row.
When I click the link the action gets called and deletes the row from the database. I also reload the data from the database and assign it to my ArrayList.
However, the init Method is also called
before the action is executed. So the
database call is fired twice when hitting the link:
- First time in the init() method of the bean
- Second time in the actionListener method when reloading the data
I can not remove the call from the actionListener, because the data has not deleted yet.
Question:*
How can I make sure the database call is fired
once only? (and also making sure the ArrayList is populated appropriate)
Maybe I am doing something wrong here? Thanks in advance for any help.
Maik
This is the request scope bean:
public class UserBean implements Serializable {
private List all;
private Long userId = null;
@PostConstruct
public void init() {
if(all == null) {
new ArrayList();
loadUserList();
}
}
/**
* Constructor
*/
public UserBean() {
super();
}
/**
* @return the userId
*/
public Long getUserId() {
return userId;
}
/**
* @param userId
* the userId to set
*/
public void setUserId(Long userId) {
this.userId = userId;
}
/**
* @param all
* the all to set
*/
public void setAll(List all) {
this.all = all;
}
public List getAll() throws GeneralModelException {
return all;
}
public void loadUserList() {
EntityManager em = Contexts.getEntityManager();
Query q = em.createNamedQuery("user.findAll");
all = q.getResultList();
}
public void deleteAction(ActionEvent ae) {
EntityManager em = Contexts.getEntityManager();
Query q = em.createNamedQuery("user.byId");
q.setParameter("id", userId);
try {
User user = (User) q.getSingleResult();
if (user != null) {
em.remove(user);
loadUserList();
}
} catch (NoResultException e) {
// TODO
}
}
}