使用jpa 2.x调用存储过程会抛出llegalArgumentException

时间:2019-03-29 08:05:21

标签: java jpa spring-data-jpa

我要从Java代码执行的mssql服务器上具有以下过程

DECLARE @RC int
DECLARE @newUid bigint

EXECUTE @RC = [TEST_DB].[foobar].[getNextUid] @newUid OUTPUT

在我的代码中,我定义了一个实体,并使用NamedStoredProcedureQuery批注定义了它

@Entity
@NamedStoredProcedureQuery(
    name = "foobar.getNextUid",
    procedureName = "foobar.getNextUid",
    parameters = {
        @StoredProcedureParameter(mode = ParameterMode.OUT, type = Long.class, name = "newUid")
    }
)

当我在测试中调用此存储过程时,它将引发异常

java.lang.IllegalArgumentException: No @NamedStoredProcedureQuery was found with that name : foobar.getNextUid

我也尝试了将注释中的参数命名为@newUid

这是我的测试用例

EntityManager entityManager = managerEntityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
StoredProcedureQuery query = entityManager.createNamedStoredProcedureQuery("foobar.getNextUid");
query.setParameter("@newUid", 1);
long nextUid = (long) query.getOutputParameterValue("newUid");

entityManager.getTransaction().commit();
entityManager.close();

1 个答案:

答案 0 :(得分:0)

尝试更换 query.getOutputParameterValue(“ @ newUid”); 通过 query.getOutputParameterValue(“ newUid”);