【问题标题】:call stored procedure JPA 2.1 Repository with Spring使用 Spring 调用存储过程 JPA 2.1 Repository
【发布时间】:2015-07-26 11:20:34
【问题描述】:

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

我已经尝试过解决方案:here 和 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 存储库上给了我一个错误:

"派生查询无效!找不到类型的属性搜索 活动上下文”

非常感谢!

【问题讨论】:

  • 你在使用 Spring Boot 吗?如果不是,您是在为 bean 或 Java 配置使用 XML 配置吗?问题看起来是您的 entityManager bean 未初始化。

标签: java spring hibernate jpa spring-data-jpa


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 2021-09-02
    • 1970-01-01
    • 2018-01-03
    相关资源
    最近更新 更多