How to use Spring Data Repositories as Vaadin JPAContainer
As far as you can get EntityProvider from JPARepository or somewhere else you can use JPAContainer like this:
EntityManager entityManager = getEntityManager(Campaign.class));MutableLocalEntityProvider<Campaign.class)> provider;provider = new CachingMutableLocalEntityProvider<Campaign.class)>(Campaign.class), entityManager);provider.setTransactionsHandledByProvider(false);JPAContainer<Campaign> container = new JPAContainer<Campaign>(Campaign.class); container.setEntityProvider(EntityProviderUtil.get().getEntityProvider(Campaign.class));
or jsut simple
EntityManager entityManager = getEntityManager(Campaign.class));JPAContainer<Campaign> container = JPAContainerFactory.make(Campaign.class, entityManager)
Well, you should read following post and decide if you did not want to use you JPARepository as a model layer and wrap it into BeanItemContainer, cuz JPAContainer looks good but has some performance issues from my point of view.
import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import com.vaadin.addon.jpacontainer.JPAContainer; import com.vaadin.addon.jpacontainer.JPAContainerFactory; @SpringComponent public class SpringDataVaadinJPAContainer { @PersistenceContext private EntityManager entityManager; public SpringDataVaadinJPAContainer() { JPAContainer<Person> container = JPAContainerFactory.make(Person.class, entityManager); } }
Spring Data is not compatible with the architecture of JPAContainer. In general I'd suggest not to use JPAContainer at all, but just get the entities from Spring Data repository and pass them for Vaadin components as such. Example:
grid.setContainerDataSource(new BeanItemContainer(Person.class, repo.findAll());
Until Vaadin 8 is out, I also suggest to use Viritin (I'm the author and also have been maintaining and developing Vaadin itself for a decade) which gives you better typing, simpler APIs and also better performance. See this Spring Data CRUD example for a full stack example app.