How can I execute a stored procedure with JPA & Spring Data?

后端 未结 2 425
情书的邮戳
情书的邮戳 2021-01-17 10:59

I am trying to call the Terminal_GetTicket stored procedure in my database but keep getting the following exception:

PropertyReferenceException:         


        
2条回答
  •  星月不相逢
    2021-01-17 11:50

    I remember that I have been struggling with the MS SQL stored procedures and spring-data-jpa. This is how I have been able to successfully run it:

    Model:

    @NamedNativeQueries({
        @NamedNativeQuery(
                name = "yourInternalName",
                query = "EXEC [procedure_name] :param1, :param2",
                resultClass = Foo.class
        )
     })
     @Entity
     public class Foo{
    
         /* Fields, getters, setters*/
    }
    

    That's pretty straightforward. This approach is different though, you are not declaring procedures directly (that's also the reason why it doesn't have to work if you decide to change RDBS).

    Then you have to extend your repository:

    public interface FooRepositoryCustom {
    
         Foo fancyMethodName(arg1, arg2);
    }
    

    And directly implement it:

    public class FooRepositoryImpl implements FooRepositoryCustom {
    
    
    @PersistenceContext
    EntityManager entityManager;
    
    @Override
    public Foo fancyMethodName(arg1, arg2) {
    
        Query query = entityManager.createNamedQuery("yourInternalName");
        query.setParameter("param1", arg1);
        query.setParameter("param2", arg2);
        return query.getResultList();
    }
    

    Let's put it all together:

    public interface FooRepository extends CrudRepository, FooRepositoryCustom {
    
    }
    

    Note that if you decide to return for example a List of Foo objects you only edit return value in your custom repository.

提交回复
热议问题