ListSelect with newItemsAllowed set to true: Transaction rolled back

Hello,

I am new both to Vaadin and this forum. I already managed to make friends with the first one, I hope my presence on this forum will also be a great experience.

I am having hard time trying to figure out how to take care of adding new emails to a JPAContainer using ListSelect with setNewItemsAllowed(true). I wrote my own NewItemHandler() and I add the items in a following way:


Email email =  new Email();
			    	email.setName(newItemCaption);
			    	email.setBpartnerId(bpartner);
			    	try {
			    		emailContainer.addEntity(email);
			    		s.setRows(emailContainer.size());
			    		s.addItem(email.getName());
			    	}
			    	catch (UnsupportedOperationException e) {
			    		
			    	}

The thing is ok when there actually is a business partner I can add these emails to. But what if I am creating a new partner? The bpartner variable is null and I get the TransactionSystemException. How can I delay adding the items to the container (add them when the whole form is submitted at the point where I want it)?

Thank you in advance :slight_smile:

No one knows the reply? I am still stuck at this…

If you have an integrity constraint that says that the partner cannot be null, you need to commit both in the same transaction.

You can either delay adding the new entity to the container until you have the partner entity ready to be stored or manage transactions yourself. Note, though, that many JPA implementations such as Hibernate do not really like long transactions spanning multiple requests and you risk locking (parts of) the database for a long time, using up many DB connections, leaking of open transactions etc. if you try to do transactions spanning multiple requests from the client.

How to delay adding the entity to the container depends on your application. It looks to me like you have two separate containers, the JPAContainer emailContainer and separately s for the list. Thus, you could just add it to the list and not to emailContainer until the form is actually committed, keeping track of the new entities somehow and adding them on commit of the partner. The tracking can even be via s if you put the entities themselves in s instead of just their names.

Hi,

Thanks for the reply. I added Bpartner entity and wanted to add emails to it after that. Here is my code:


if (ok) {
				saveButton.setComponentError(null);
				EditorSavedEvent ese = new EditorSavedEvent(this, item, isNew);
				fireEvent(ese);
				if (ese.isNewEntity()) {
					BeanItem<Bpartner> bpartner = (BeanItem<Bpartner>)ese.getSavedItem();
					dataForm.modifyEmails(bpartner.getBean()); // Add emails
				}
			} else {
				saveButton.setComponentError(new UserError("Błąd zapisu"));
			}

The problem is that getSavedItem() returns an incomplete Item, it does not have an id. Why is that? I guess there is some detaching on the way but where and why?

This is really urgent, please help!