如何用MySQL解决Hibernate中存储函数调用的返回值?

时间:2017-12-04 10:09:10

标签: java mysql spring hibernate jboss

我正在尝试从hibernate中的MySQL存储过程中获取数据。我有一个custom_query.xml文件和DAO.java文件

MySQL中的

存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `SET_NETWORK`(
    IN TO_CREATE BIGINT,
    OUT CREATED BIGINT
)
BEGIN
     SET CREATED = (SELECT COUNT(VNR.ID) FROM NETWORK_TABLE VNR);
    WHILE (CREATED < TO_CREATE) DO
      INSERT INTO NETWORK_TABLE (ACTIVE) VALUES(0);
      SET CREATED = CREATED + 1;
    END WHILE;
END

我正在尝试使用以下代码从hibernate执行SQL查询:

 <sql-query name="Network_getFreeNetworksForDomain" read-only="false" callable="true" cacheable="false"><![CDATA[
        { ? = CALL SET_NETWORK(?,?) }
    ]]></sql-query>

在DAO.java文件中,我使用Network_getFreeNetworksForDomain来调用SQL查询:

@Override
    @Transactional(readOnly = true)
    @SuppressWarnings({"unchecked"})
    public List<Network> getNetworksForDomain(Domain domain) throws DataAccessException {
        Long domainId = domain.getId();
        if (domainId == null) {
            domainId = 0L;
        }
        return (List<Network>) getSession().getNamedQuery("Network_getFreeNetworksForDomain")
                .setLong("dId", domainId).list();
    }

但是我在将文件部署到JBoss时正在进行排泄

 Caused by: java.sql.SQLException: Can't set IN parameter for return value of stored function call.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) [mysql-connector-java-5.1.6.jar:]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) [mysql-connector-java-5.1.6.jar:]
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) [mysql-connector-java-5.1.6.jar:]
        at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3294) [mysql-connector-java-5.1.6.jar:]
        at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3272) [mysql-connector-java-5.1.6.jar:]
        at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3314) [mysql-connector-java-5.1.6.jar:]
        at com.mysql.jdbc.PreparedStatement.setLong(PreparedStatement.java:3330) [mysql-connector-java-5.1.6.jar:]
        at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.setLong(WrappedPreparedStatement.java:229)
        at com.dao.entity.NetworkHibernateDAO$1.createCallableStatement(NetworkHibernateDAO.java:169) [dao-impl-6.4.0.jar:]
        at com.dao.hibernate.transaction.HibernateUtils.execute(HibernateUtils.java:106) [dao-impl-6.4.0.jar:]
        at com.dao.hibernate.transaction.HibernateUtils$1.doInConnection(HibernateUtils.java:74) [dao-impl-6.4.0.jar:]
        at com.dao.hibernate.transaction.HibernateUtils$2.execute(HibernateUtils.java:91) [dao-impl-6.4.0.jar:]
        at org.hibernate.impl.SessionImpl.doWork(SessionImpl.java:1987) [hibernate-core-3.5.6-Final.jar:3.5.6-Final]
        ... 51 more

 Caused by: org.springframework.orm.hibernate3.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [???]; SQL state [S1009]; error code [0]; error executing work; nested exception is org.hibernate.exception.GenericJDBCException: error executing work

有什么建议吗?如何解决这类错误?

更新:

在DB2和PostgreSQL中,这可以正常使用

 <sql-query name="Network_getFreeNetworksForDomain" read-only="false" callable="true" cacheable="false"><![CDATA[
            { ? = CALL SET_NETWORK(?) }
        ]]></sql-query>

但是得到了上面提到的同样的例外。所以我在mys存储过程中添加了CALL SET_NETWORK(?,?)两个参数。

1 个答案:

答案 0 :(得分:0)

  

{? = CALL SET_NETWORK(?,?)}

似乎你需要2个参数而你只提供了一个

  

.setLong(“dId”,domainId)

我不确定你的代码能否找到“dId”参数,也许你应该使用索引,你可以有一些样本in the official documentation