d0x
d0x

Reputation: 11581

How to use Spring Data Repositories as Vaadin JPAContainer

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.

Upvotes: 10

Views: 3539

Answers (3)

mstahv
mstahv

Reputation: 1934

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.

Upvotes: 0

tsogtgerel.ts
tsogtgerel.ts

Reputation: 975

    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);

        }
    }

Upvotes: 1

Milan Baran
Milan Baran

Reputation: 4232

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

Upvotes: 1

Related Questions