javax.persistence.Persistence has no getEntityManager() method

I am trying to create simple repository. But can’t get EntityManager instance.

As mentioned in documentation I create new component, but javax.persistence.Persistence has no getEntityManager() method.
And I don’t see any other Persistence anywhere in dependencies.

@PersistenceContext EntityManager em

doesn’t work eather.

My full code of repository:


import org.springframework.stereotype.Component;
import javax.inject.Inject;
import javax.persistence.Persistence;

@Component(CustomUserRepository.NAME)
public class CustomUserRepository {
    public static final String NAME = "cuba_CustomUserRepository";

    @Inject
    private Persistence persistence;

    public void findUserByEmbriaId(String embriaId) {
        persistence.getEntityManager();        // Compilation error. There are no getEntityManager()
    }

}

I contain repository in app-global, and trying to use it from app-web, if it matters.

It matters. Persistence and EntityManager interfaces can be used only on the middle tier, i.e. in the core module. See https://doc.cuba-platform.com/manual-6.4/middleware.html.

Also, please notice that you should use these interfaces from the com.haulmont.cuba.core package, javax.persistence won’t work.

2 Likes