0
votes

In my jsf bean's init method I'm making a call to the service which fetches the object from the database. We are using hibernate. This init method prepopulates values on the form. The user goes makes edits and on submit another method is called to capture the edits and update that object in the database. Now at this point I can store the database object retrieved during init method as the property on the backing bean or I can in the submit method called in the jsf bean again retrieve the original database object (but that is another call to the db/cache). Is there a preferred way here ?

2

2 Answers

0
votes
  1. Put the retrieved entity instance into the session.
  2. Make your form fields' targets to every mapped to field using value="#{sessionScope.xxx.xxx}"
  3. On submit you can merge or persist the instance in the session.
0
votes

The first way (to store the detached entity and merge it on submit) is better imo, especially when used with a version field (see Java - JPA - @Version annotation) because it provides a way to detect stale entities.