spring-boot,jpa,@query,sybase存储过程

t1rydlwq  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(336)

尝试使用jpa@query进行sybase存储过程调用。我在关注这里提供的信息,https://www.baeldung.com/spring-data-jpa-stored-procedures,作为指导原则,尽管信息是针对mysql和命名参数的。我知道存储过程可以工作,因为我可以使用客户机dbvis执行它。我还知道存储过程的值是正确的,因为我打开了hibernate日志记录,每次调用前都会看到以下代码段:

2020-12-30 13:51:07 TRACE o.h.t.d.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [pub_req_t]
2020-12-30 13:51:07 TRACE o.h.t.d.sql.BasicBinder - binding parameter [2] as [INTEGER] - [1]

@query如下所示:

@Query(value = "call rp_surrogate(?1, ?2);", nativeQuery = true)
int getSurrogateByQueryV1(String surrtype, Integer newsurr );

存储过程声明是:

CREATE PROCEDURE dbo.rp_surrogate (
    @surrtype       char(32),
    @newsurr        domn_num_id    output
)
as begin

我传入?2(output)参数的内容似乎无关紧要,因为它被忽略了。我试过jconn4,版本7.0,和jtds,版本1.3.1,jdbc驱动程序…都产生了相同的异常:
jconn4-原因:com.sybase.jdbc4.jdbc.sybsqlexception:“@p0”附近的语法不正确。
jtds-原因:java.sql.sqlexception:“@p0”附近语法不正确。
运行时堆栈:
Spring Boot2.2.8
java 1.8版
sybase酶15
我试过将@modifying annotation与@trasactional结合使用:我认为jpa可能会将@p0用作rowsupdated或存储过程的状态码……不。

@Modifying
@Query(value = "call rp_surrogate(?1, ?2);", nativeQuery = true)
@Transactional(rollbackFor=Exception.class)
int getSurrogateByQueryV1(String surrtype, Integer newsurr );

Results in : Caused by: com.sybase.jdbc4.jdbc.SybSQLException: Incorrect syntax near '@p0'.

另外,在@query上尝试了4种变体,都返回了相同的异常…有些带有分号,有些带有调用。

@Query(value = "call rp_surrogate(?1, ?2)", nativeQuery = true)
int getSurrogateByQueryV1(String surrtype, Integer newsurr );

Results in : Caused by: java.sql.SQLException: Incorrect syntax near '@P0'.

@Query(value = "call rp_surrogate(?1, ?2);", nativeQuery = true)
int getSurrogateByQueryV2(String surrtype, Integer newsurr );

Results in : Caused by: java.sql.SQLException: Incorrect syntax near '@P0'.

@Query(value = "rp_surrogate(?1, ?2);", nativeQuery = true)
int getSurrogateByQueryV3(String surrtype, Integer newsurr );

Results in : Caused by: java.sql.SQLException: Incorrect syntax near '@P0'.

@Query(value = "rp_surrogate(?1, ?2)", nativeQuery = true)
int getSurrogateByQueryV4(String surrtype, Integer newsurr );

Results in : Caused by: java.sql.SQLException: Incorrect syntax near '@P0'.

我能报告的唯一有效的方法是,下面的方法是有效的,但是我们希望有一个使用@query的干净/精简版本,因为我们需要调用的一些过程的参数相当大。

@Override
public int getSurrogateByType(String typeSurrogate) {
    EntityManager em = secondaryEntityManager.getObject().createEntityManager();

    StoredProcedureQuery query = em.createStoredProcedureQuery("rp_surrogate");
    // JDBC : The jconn and jtDS drivers do NOT understand named parameters...we
    // MUST use positional...
    //query.registerStoredProcedureParameter("surrtype",     String.class,     ParameterMode.IN    );
    //query.registerStoredProcedureParameter("newsurr",     Long.class,     ParameterMode.OUT    );
    query.registerStoredProcedureParameter(1, String.class,     ParameterMode.IN);
    query.registerStoredProcedureParameter(2, Integer.class,    ParameterMode.OUT);
    getLog().error("getSurrogateByType() - Surrogate Type [{}]", typeSurrogate);
    query.setParameter(1, typeSurrogate);

    try {
        query.execute();
    } catch (Exception ltheXcp) {
        getLog().error("{}() - process failure, with exception(s) : ", this.getClass().getSimpleName(), ltheXcp);
        ltheXcp.printStackTrace();
        throw ltheXcp;
    }

    Integer commentCount = (Integer) query.getOutputParameterValue(2);
    return commentCount.intValue();

}

有人知道如何使用sybase驱动程序和@query进行这样的调用吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题