Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
JPAContainer- how to get the id of commited item
Hi,
we use the jpacontainer 2.0 with eclipselink and postgres.
I need to know, how i get the generated id from an new created item after calling jpacontainer.commit()?
I supposed that the item will be updated after commit with the generated id, but the id is still 0 and item.isPersistent() returns false.
Any suggestions for that issue?
Are you trying to get it from the new container item or from the entity that you added? If you try to get it from an entity that you've added with addEntity(), you need to get the persisted entity from the container using the item ID that you get with addEntity().
Hmm, ok i have try it, but the JPAContainer does not know the generated UUID. After some investigation i have found the function clear() in BufferdeContainerDelegate. This function delets all known items from the JPAContainer. And this function will be called at the end of commit();
180 private void clear() {
181 deltaList.clear();
182 addedEntitiesCache.clear();
183 addedItemIdsCache.clear();
184 updatedEntitiesCache.clear();
185 deletedItemIdsCache.clear();
186 }
So, if i called getItem() with the UUID after the commit(), the cache is empty and the result is an Exception: "You have provided an instance of an incorrect PK class for this find operation. Class expected : class java.lang.Long, Class received : class java.util.UUID."
Hello Andre,
maybe a little bit too late, but maybe others are interested in the answer as well. When you are using JPAContainer with a buffered entity provider (such as CachingBatchableLocalEntityProvider - which is the default one, I think), you get back an UUID as Item-ID from the JPAContainer, as long as you do not do a commit on the container. After that you have to use the ID given by the database to retrieve your EntityItem again.
Example:
JPAContainer<Contact> contactContainer = JPAContainerFactory.make(...);
Contact contact = new Contact();
contact.setDepartment("xyz");
contact.setEmail("");
contact.setGender(Contact.GenderListing.MALE);
contact.setName("Abc");
contact.setPhone("1234");
UUID uuid = (UUID) contactContainer.addEntity(contact);
EntityItem<Contact> contactEntityItem = contactContainer.getItem(uuid); // use UUID to retrieve the EntityItem
contactContainer.commit(); // *now* the entity item is persisted to the db
EntityItem<Contact> item2 = contactContainer.getItem(contact.getId()); // *now* we have to use the ID of the database
Hope this helps,
Victor