使用 Spring 调用存储过程 JPA 2.1 Repository

call stored procedure JPA 2.1 Repository with Spring

我正在尝试在 MySQL 调用中调用存储过程:"searchContext"。

我试过解决方法:here and here

但是我怎样才能得到 EntityManager 引用呢?如果我像这样使用 JPA 存储库:

public interface ActiveContextRepository extends JpaRepository<ActiveContext, Long> {
    ActiveContext findByIdUser(Integer idUser);     
}

当我像这样使用 EntityManager 时得到空指针:

public class ContextService {
    @PersistenceContext
    protected EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public Integer getContext(Integer idUser, String date, Double longitude, Double latitude){
             StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("searchContext");
}

或者像这样:

public class ContextService {

    @Autowired EntityManager entityManager;

    public Integer getContext(Integer idUser, String date, Double longitude, Double latitude){
                 StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("searchContext");
}

我也试过这个解决方案:here 但是预编译器在 Jpa 存储库上给我一个错误:

"Invalid derived query!No property search found for a type ActiveContext"

非常感谢!

我已经解决了这个问题。我正在制作 class 的 "new() ",它有新的实体管理器...nie 被注入 "autowire" 并且 bean 在 application-config 中声明谢谢