【问题标题】:MySQL and import stored procedure on Hibernate start-upHibernate 启动时 MySQL 和导入存储过程
【发布时间】:2014-04-21 20:46:57
【问题描述】:

我想在运行我的 java 程序时更新我的​​所有 SP(如果这不是好的做法也没关系)。 由于我使用的是 Hibernate 4.3.2.Final,因此我试图避免为 MySQL 编写自己的 SQL 执行器。 所以我试图在我的 hibernate.cfg.xml 中使用 hibernate.hbm2ddl.import_files。 它获取脚本,但无法解析它。我试过了

    <property name="hibernate.hbm2ddl.import_files">/Database/stored_procedures/load.sql</property>
    <property name="hibernate.hbm2ddl.import_files_sql_extractor">org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor</property>

但我明白了

org.hibernate.tool.hbm2ddl.ImportScriptException: Error during statement execution (file: '/Database/stored_procedures/load.sql'): DELIMITER $$ 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$ 

SP sql 文件在 MySQL 上运行良好(尝试了很多次),类似于:

DROP PROCEDURE IF EXISTS ebull.`load_friends`;

DELIMITER $$ 

CREATE PROCEDURE ebull.`load_friends`(
    IN transactionId BIGINT,
    IN chunk INT
)
BEGIN
    ...sql queries and stuff....

END

$$
DELIMITER ;

我认为分隔符有问题,但我不知道如何避免它们,因为它是一个存储过程定义。 我开始认为这是不可能的。有什么想法吗?

【问题讨论】:

    标签: mysql sql hibernate file import


    【解决方案1】:

    在 Java 中,您无需在 SQL 脚本中使用基于 Delimiter 的语法来创建存储过程。修改您的 SQL 脚本文件以删除 DELIMITER ... 部分并从 JAVA 运行其余的过程代码。

    参考Creating Stored Procedure in MySQL with SQL Scripts or JDBC API

    【讨论】:

    • 感谢您的回答,但没有奏效。相同的语法错误。 Error during statement execution (file: '/Database/stored_procedures/load_friends.sql'): CREATE PROCEDURE ebull.load_friends( Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 9 另一个奇怪的是没有第 9 行:O 最后一行是第 6 行
    • 我认为问题出在分隔符上,它试图执行 SP 内部的内容,但不应该因为这是存储过程的一部分!它应该创建 sp,因此请遵循 CREATE PROCEDURE 语句,而不是尝试执行其中的内容。
    • 在用这个 sp:DROP PROCEDURE IF EXISTS ebull.load_friends; CREATE PROCEDURE ebull.load_friends(IN transactionId BIGINT, IN chunk INT) BEGIN CALL ebull.load_friends_user(transactionId,chunk); CALL ebull.load_friends_base(transactionId,chunk); END; 调试休眠代码后,它正确执行了 drop 过程语句,但是休眠尝试执行所有内容直到下一个分号,这是错误的,因为该代码的一部分是一部分SP 代码!
    猜你喜欢
    • 2015-11-08
    • 2011-04-18
    • 2017-08-24
    • 2012-05-08
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 2019-10-22
    • 2012-12-30
    相关资源
    最近更新 更多