0

I'm trying to call a stored procedure in MySQL call: "searchContext".

I have tried the solution : here and here

But how can I get the EntityManager reference? If I'm using JPA Repositories like this:

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

I got null pointer when I use EntityManager like this:

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");
}

Or like this:

public class ContextService {

    @Autowired EntityManager entityManager;

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

I also haved tried this solution:here But precompiler gives me an error on the Jpa repository:

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

Thank you very much!

Community
  • 1
  • 1
J.F.
  • 307
  • 1
  • 4
  • 18
  • Are you using Spring Boot? If not are you using XML configuration for beans or Java Configuration? Problem looks to be your entityManager bean not initialized. – Nitin Arora May 19 '15 at 08:06
  • Hell, You can have a look [here](https://stackoverflow.com/questions/3572626/how-to-call-a-stored-procedure-from-java-and-jpa/70788256#70788256), I hope it helps : [How to call a stored procedure from Java and JPA SQL Server](https://stackoverflow.com/questions/3572626/how-to-call-a-stored-procedure-from-java-and-jpa/70788256#70788256) – RED-ONE Jun 05 '23 at 22:47

1 Answers1

0

I have resolved the problem. I was making a "new() " of the class wich have the new entity manager...nie is injected with "autowire" and bean is declared in application-config thank you

J.F.
  • 307
  • 1
  • 4
  • 18