Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Usage of multiple JPA classes in Vaadin CDI Example
I use the cdi eample with vaadin at https://github.com/vaadin/cdi/tree/master/vaadin-cdi-example.
This example uses a service bean to retrieve objects via JPA from the database.
The service injects a persistence context as follows:
@PersistenceContext(unitName = "example")
private EntityManager entityManager;
This bean is injected in the presenter (managing the form, etc) as follows:
private CustomerService customerService;
However, the example just has one class, namely customer.
How would this work in case of multiple JPA classes?
Should there be many service beans, e.g. one per JPA class?
And if so, does this result in multiple persistence contexts?
J G: Should there be many service beans, e.g. one per JPA class?
Depends completely on how you architecture your application and what are the needs. You could have one service for each JPA class, but then again, in anything except trivial examples, JPA classes are connected with each other, so it most likely won't make sense to have one service per JPA entity. There are different approaches you can make, you can have a service based architecture, so all "related" tasks are in the same service, regardless of the JPA class. You could also do a domain driven design, where you don't have any services in the same way, but rather, everything revolves around domain models.
There's no best practice, it completely depends on what you want to achieve, sorry that I cannot give you a "do like this" answer :)
J G: And if so, does this result in multiple persistence contexts?
In this case, no. Use the same EntityManager to manage both entities.
How do I ensure that I just use one EntityManager for all service beans?
Right now, I made one superclass for all the beans containing the declaration of the EntityManager.
But I assumed that multiple service beans result in multiple instances, to in the end in multiple EntityManagers.
Sorry, I left a vital bit of information out :) So no, there will not be multiple persistence units within the same service class, but in another service class, you might get a different instance. Thumb rule is that you can leave that pretty much up to the container to handle, just inject an EntityManager to your second service class and you should be all set to go.
Side note, depending a bit on which JPA provider you are using, you might run into different issues with Vaadin and JPA. JPA is meant for stateless applications while Vaadin is stateful. Personally, I've found EclipseLink to work best together with Vaadin. I do have some collagues that prefer Hibernate, although, it doesn't work equally well out-of-the-box as EclipseLink does.
Yes, I already discovered that Hibernate does lazy loading, and so requires additions to have eager loading. Eclipselink seems to handle this transpartantly.