@UiScope'd backing MVP which contain @UiScope'd EntityManager/Session, with Spring

I’m thinking about such architecture. instead of having per-request entity manager handled by Spring (Data), to have an UiScoped entityManager that is open but disconnected at the end of each UI event (by commit/rollback), and (re)connected on each interaction (with new transaction started). It would allow to keep the entity beans “attached” to the (disconnected) entity manager for the life of the UI screen, making much more natural development model.

is this too crazy? I guess similar thing can be achieved with stateful EJBs in CDI world

I’m doing some tests by having a view like:

public class AboutView extends VerticalLayout {

    WeirdoBean weirdoBean;

and defining WeirdoBean as

public class WeirdoBean {
    private final Session session;
    private LegalEntity legalEntity;

    public WeirdoBean(EntityManagerFactory entityManagerFactory) {
        this.session = (Session) entityManagerFactory.createEntityManager().getDelegate();

then on the UI interaction (eg: button click) I’m doing:

    public void jaja() {
        System.out.println("XXXXXX begin");
        var tx = session.beginTransaction();
        this.legalEntity = session.createQuery("select l from LegalEntity l", LegalEntity.class).setMaxResults(1).getResultList().get(0);
        System.out.println("XXXXXX committing");
        System.out.println("XXXXXX committed");

then on a subsequent interaction I do:

    public void jojo() {
        var tx = session.beginTransaction();
        legalEntity.getCompanies().forEach(company -> {
            System.out.println("company = " + company.getSlug());

and I’m actually getting the “companies” collection initialized properly (whereas in previous method it wasn’t initialized)

actually I’m seeing that I should do disconnect/reconnect on every “UI” interaction.

is there any way I can hook on a @UiScope bean to listen to some kind of Event (Spring event? some interface?) that informs when an interaction has been dispatched (eg: the handling of a button click has been finished) so that I can force a disconnect there?

am I heading a road to misery this way? :smile:

Please note that the UI is around for the duration of the session, I’m not sure that’s what you want.

In most cases, you probably want to create some sort of DTOs that you use between your services and the UI (not using attached entities directly in the views), so it may not be as relevant if the entities stay attached in the service layer

this crazy idea was to make it possible to avoid the dtos and use entities directly without initialization errors :slightly_smiling_face:

This crazy idea is so crazy that you shouldn’t do it in an enterprise grade application with a long lifespan :grimacing:

I think your future self will thank you if you don’t do it :smile:


just experimenting, I was thinking on stateful beans

and if it could be applied as backing bean of a vaadin app page

With JOOQ you can use the entities (in JOOQ records ) directly in the UIScoped views. And even better, you button listener can directly call record.store() to write into the DB. No more hassle with all those entity state boundaries. But it‘s a medium huge shift from Hibernate/ JPA to JOOQ … but it‘s worth it.

I know… investigated it recently too. Thanks for the hint. :smile:

saw there is even a helper in vaadin directory for using it

@adaptable-uakari is a big jooq prpoponent :slightly_smiling_face: