如何在 JPA/Hibernate 中执行本机 SQL 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14358425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:10:28  来源:igfitidea点击:

How can I execute a native SQL script in JPA/Hibernate?

sqloraclehibernatejpa

提问by Anke

I have a SQL script with database dump. How can I execute it using Hibernate's EntityManager?

我有一个带有数据库转储的 SQL 脚本。如何使用 Hibernate 执行它EntityManager

I tried it this way:

我是这样试的:

EntityManager manager = getEntityManager(); 
Query q = manager.createNativeQuery(sqlScript);
q.executeUpdate();

but it works only when sqlScriptcontains a single SQL query, while I need to run multiple inserts and other complex stuff.

但它仅在sqlScript包含单个 SQL 查询时才有效,而我需要运行多个插入和其他复杂的东西。

RDBMS: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

RDBMS:Oracle 数据库 11g 快捷版 11.2.0.2.0 版 - 64 位生产

回答by hkutluay

Wrap your query with begin end block. Like

用开始结束块包装您的查询。喜欢

EntityManager manager = getEntityManager(); 
Query q = manager.createNativeQuery("BEGIN " + sqlScript + " END;");
q.executeUpdate();