Yes!
Yes! it is and Update and then update again
I’ll try to be helpful
I showed version field on the Object List and on the Object Form (opened as Window), as soon the object is updated (by calling item.commit()), the version field on the Object List is updated to new value, while not on the form.
I also printed the following walues, before and after the commit, but the value printed is unchanged.
System.out.println("1 " + item.getItemProperty("version").getValue());
System.out.println("2 " + form.getField("version").getValue());
System.out.println("3 " + item.getEntity().getVersion());
System.out.println("Commit");
item.commit();
System.out.println("1 " + item.getItemProperty("version").getValue());
System.out.println("2 " + form.getField("version").getValue());
System.out.println("3 " + item.getEntity().getVersion());
Basically what I did is to keep the jpacontainer demo as-is, just changed the behavior of the form, that after commit was called does not close the window, so the user can update the object more than once (without re-opening again from the Object List), and obviously I changed Business Objects,.
I’ll report much representative codes
GuestBookList
public class GuestBookList extends BaseWindow {
private final Table table = new Table();
final Button openButton = new Button("Open");
@Resource(name = "guestBookProvider")
private EntityProvider<GuestBook> entityProvider;
private JPAContainer<GuestBook> container = new JPAContainer<GuestBook>(GuestBook.class);
public GuestBookList() {
setCaption("GuestBook");
setIcon(new ThemeResource("icons/16/comments.png"));
}
@PostConstruct
public void init() {
openButton.setIcon(new ThemeResource("icons/16/comment_edit.png"));
openButton.addListener(new Button.ClickListener() {
@Override
public void buttonClick(final ClickEvent event) {
final Object itemId = table.getValue();
if(itemId != null) {
final EntityItem<GuestBook> item = container.getItem(itemId);
if(item == null) getWindow().showNotification("GuestBook deleted by another user");
else getWindow().addWindow(new GuestBookForm(item));
}
}
});
getMenuBar().addComponent(openButton);
container.setEntityProvider(entityProvider);
container.setApplyFiltersImmediately(false);
container.setAutoCommit(true);
container.setContainsIdFiresItemSetChangeIfNotFound(true);
table.setSizeFull();
table.setContainerDataSource(container);
table.setVisibleColumns(new String[] { "version", "id", "firma", "data", "descrizione" });
table.setColumnHeaders(new String[] { "version", "ID", "Firma", "Data", "Descrizione" });
table.setColumnCollapsingAllowed(true);
table.setColumnReorderingAllowed(true);
table.setSelectable(true);
table.setImmediate(true);
addComponent(table);
}
}
GuestBookForm
public class GuestBookForm extends Window {
public GuestBookForm(final EntityItem<GuestBook> item) {
setCaption(item.getEntity().toString());
setIcon(new ThemeResource("icons/16/comment_edit.png"));
item.setWriteThrough(false);
setModal(true);
final Form form = new Form();
{
form.setWriteThrough(true);
form.setItemDataSource(item);
form.setVisibleItemProperties(new String[] { "id", "version", "firma", "data", "descrizione" });
addComponent(form);
final Button save = new Button("Save", new Button.ClickListener() {
@Override
public void buttonClick(final ClickEvent event) {
try {
form.validate();
try {
System.out.println("1 " + item.getItemProperty("version").getValue());
System.out.println("2 " + form.getField("version").getValue());
System.out.println("3 " + item.getEntity().getVersion());
System.out.println("Commit");
// This throws
// javax.persistence.OptimisticLockException: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [it.oredelmondo.webstore.domain.GuestBook#1]
item.commit();
System.out.println("1 " + item.getItemProperty("version").getValue());
System.out.println("2 " + form.getField("version").getValue());
System.out.println("3 " + item.getEntity().getVersion());
System.out.println();
if (item.getItemId() == null) item.getContainer().addEntity(item.getEntity());
} catch (final Exception e) {
form.getParent().getWindow().showNotification("Meaage: ", e.getMessage(), Notification.TYPE_ERROR_MESSAGE);
}
} catch (final InvalidValueException e) {}
}
});
save.setIcon(new ThemeResource("icons/16/disk.png"));
form.getFooter().addComponent(save);
}
setSizeUndefined();
setWidth("80%");
}
}