1

I need to update an entity in my managed bean. I want to do this by passing this entity to a method in my managed bean with a MethodExpression, with action or actionListener, which seems like the easier and clearest way. I want the managed bean to have view scope because I'm using Primefaces' dataTable and it doesn't seem to call the action method when I change it to request scope (just my preRenderView listener).

<h:commandXXXX action="#{bean.method(entity)}">

@ManagedBean(name="bean")
@ViewScoped
class Bean {
    public void method(Entity entity) {
        // ...
    }
}

The problem is, this entity might have been changed by another user or just the same user in another page. I could retrieve the entity from the database again and overwrite the argument, but this feels silly and ugly.

public void method(Entity entity) {
    entity = entityDAO.findById(entity.getId());
}

Is there a better way to do this? Would I have the newest state of the entity if I were to change the bean to request scope (assuming that I am able to make it work with the dataTable)?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
zootropo
  • 2,441
  • 3
  • 31
  • 48
  • 1
    You can put your entity in application scope – user3489875 Sep 24 '14 at 09:45
  • I admit that I just update from DB :-) but you might create another EJB layer that would hold the entity attached to the persistence context. In this case you will need to design the transactions properly. – Yamada Sep 24 '14 at 12:29
  • 1
    This is not necessarily a JSF/Web app issue. Managing concurrent access to a single entity transcends the web application and into the data layer. **Do not put that entity (or any entity) in the application scope**. – kolossus Sep 25 '14 at 17:05
  • 1
    This is a concurrency issue and you have some very interesting answers on that [here](http://stackoverflow.com/questions/3564877/how-do-you-manage-concurrent-access-to-forms). A very simple solution would be to add a [lastupdate](http://stackoverflow.com/questions/1745890/last-update-timestamp-with-jpa) column to your entity and before saving you check if it has changed. If so, you reject the save. – ForguesR Sep 26 '14 at 01:58

0 Answers0