JPA 2.1 StoredProcedureCall似乎对我来说很麻烦

时间:2014-04-04 15:34:48

标签: java jpa jpa-2.1

无论是否有效,即使错误是完全其他问题,也要一直说:

Parameter number 2 is not an OUT parameter

即使存储过程不存在,例如:

Java调用:

StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("`sequence`.next_totally_not_existed_valsafafasf");

例外:

Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Parameter number 2 is not an OUT parameter
Error Code: 0
Call: { CALL `sequence`.next_totally_not_existed_valsafafasf(?, ?) }
    bind => [belso_ugy_iktato_szam_sequence, => 2]

MySql代码:

DELIMITER $$
CREATE PROCEDURE `sequence`.nextval (IN seq_name varchar(100), OUT cur_val BIGINT)
BEGIN
    SELECT
        cur_value INTO cur_val
    FROM
        sequence
    WHERE
        name = seq_name;

IF cur_val IS NOT NULL THEN
        UPDATE
            sequence
        SET
            cur_value = IF (
                (cur_value + increment) > max_value OR (cur_value + increment) < min_value,
                IF (
                    cycle = TRUE,
                    IF (
                        (cur_value + increment) > max_value,
                        min_value, 
                        max_value 
                    ),
                    NULL
                ),
                cur_value + increment
            )
        WHERE
            name = seq_name;
    END IF; 
END $$

Java代码:

 entityManager.getTransaction().begin();
        StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("`sequence`.nextval);

        // set parameters
        storedProcedure.registerStoredProcedureParameter("seq_name", String.class, ParameterMode.IN);
        storedProcedure.registerStoredProcedureParameter("cur_val", String.class, ParameterMode.OUT);
        storedProcedure.setParameter("seq_name", sequence_name);

        // execute SP
        storedProcedure.execute();
        // get result

        Long result = null;
        try {
            result = Long.valueOf((String) storedProcedure.getOutputParameterValue("cur_val"));
        } catch (NumberFormatException ne) {
            return 0;
        }

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

有人经历过同样的经历吗?或者我错过了什么?!

0 个答案:

没有答案