10

Is it possible to use the org.springframework.data.jpa.repository.JpaRepository Repositories as JPAContainer for Vaadin?

We are setting up a new Vaadin 7 Project from scratch with Spring 3.2.

The Spring integration is done with Spring Vaadin Integration Addon.

Rene Herget
  • 1,506
  • 13
  • 28
d0x
  • 11,040
  • 17
  • 69
  • 104
  • Chris, have you found a solution? I'm struggling with the same issue and currently I load a list of entities, create a BeanContainer, add entities inside in a foreach cycle and add it to the table. Is there a better way? – jirka.pinkas Sep 22 '13 at 17:54
  • Nope, I need to instantiate the JPAContainer by myself. I autowire the EntityMangerFactory and then pass it to a new JPAContainer – d0x Sep 24 '13 at 09:28

3 Answers3

1

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.

JPAContainer issues and different approach

MVP pattern and POJO binding with Hibernate

Community
  • 1
  • 1
Milan Baran
  • 4,133
  • 2
  • 32
  • 49
  • 1
    My goal is to get a JPAContainer from the application Context. E.g., spring instantiates a JPAContainer for every JPARepository. I don't like to instantiate them by myself. – d0x Sep 06 '13 at 17:02
1
    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);

        }
    }
tsogtgerel.ts
  • 955
  • 1
  • 15
  • 32
0

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.

mstahv
  • 1,784
  • 9
  • 7