Loading...
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.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

JPAContainer- how to get the id of commited item

Andre Peters
10 years ago Feb 24, 2012 7:40am
Marko Grönroos
10 years ago Mar 01, 2012 1:20pm
Andre Peters
10 years ago Mar 02, 2012 9:40am
Victor Röder
7 years ago Jun 05, 2014 7:29am

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

Kim Zeevaarders
7 years ago Jun 06, 2014 8:32am