是否可以从hibernate动态添加存储过程?

时间:2015-06-10 05:37:12

标签: java mysql sql hibernate stored-procedures

我想使用hibernate从我的Java代码动态添加存储过程。 我做了以下但是这经常导致错误说“

  

您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以获得正确的语法

我做错了什么?

String dnyLockProcedure = "DELIMITER ||" 
                        +"\n DROP PROCEDURE IF EXISTS `dyn_lock`; "
                        +"\n CREATE PROCEDURE `dyn_lock`(IN `query` TEXT, IN `lock_name` CHAR(64), IN `timeout` TINYINT UNSIGNED)"
                        -- PROCEDURE HERE 
                        +"\n END;"
                        +"\n || "
                        +"\n DELIMITER ;";

                Query queryTest = mEntityManager.createNativeQuery(dnyLockProcedure);
                queryTest.executeUpdate();

编辑:以下是整个程序:

        String dnyLockProcedure ="DROP PROCEDURE IF EXISTS `dyn_lock`; "
                +" CREATE PROCEDURE `dyn_lock`(IN `query` TEXT, IN `lock_name` CHAR(64), IN `timeout` TINYINT UNSIGNED)"
                +" NO SQL "
                +" LANGUAGE SQL "
                +" COMMENT 'Get a lock for dyn cursor' "
                +" BEGIN "
                +" DECLARE lock_res TINYINT UNSIGNED;"
                +" DECLARE CONTINUE HANDLER"
                +" FOR SQLEXCEPTION"
                +" SIGNAL SQLSTATE VALUE '45000' SET"
                +" MESSAGE_TEXT  = '[dyn_lock] Dynamic SQL returned an error';"
                +" SET lock_res = GET_LOCK(`lock_name`, IFNULL(`timeout`, 5));"
                +" IF (lock_res IS NULL) THEN"
                +" SIGNAL SQLSTATE VALUE '45000' SET"
                +" MESSAGE_TEXT  = '[dyn_lock] Could not acquire lock: Unknown error';"
                +" ELSEIF (lock_res = 0) THEN"
                +" SIGNAL SQLSTATE VALUE '45000' SET"
                +" MESSAGE_TEXT  = '[dyn_lock] Could not acquire lock: Timeout expired';"
                +" END IF;"
                +" SET @dyn_sql = CONCAT('CREATE OR REPLACE VIEW `', lock_name, '` AS ', query, ';');"
                +" PREPARE stmt_dyn_view FROM @dyn_sql;"
                +" EXECUTE stmt_dyn_view;"
                +" DEALLOCATE PREPARE stmt_dyn_view;"
                +" END;";

2 个答案:

答案 0 :(得分:1)

所以我发现上面的存储过程出了什么问题:

  1. 不需要使用DELIMITER,首先是CREATE PROCEDURE
  2. 使用" \ n"也不是必需的,但如果使用它也不会提示错误。
  3. 您不能在同一查询中使用DROP和CREATE;它们应该从单独的查询中调用。
  4. 干杯!

答案 1 :(得分:0)

尝试使用此代码。我希望你的问题能够得到解决。

public final List<CoverageEntity> getCMDataTable(final String countryCode, final CoverageBean coverageBean) {

            getHibernateTemplate().execute(new HibernateCallback() {
                public (List) doInHibernate(final Session session)throws HibernateException, SQLException {
//                  Query query = getQuery(countryCode, coverageBean , session);
                    Query query = session.createSQLQuery("call ASPECT.SC_CVRG_SEARCH('99991',null,null,null,null,null,null)").addEntity(CoverageEntity.class);
                    System.out.println(ToStringBuilder.reflectionToString(query.list(),ToStringStyle.MULTI_LINE_STYLE));

                    return query.query.list();

        }
            })
           ;
            return new  ArrayList<CoverageEntity>();
}