我如何在JPA / Hibernate中执行原生SQL脚本? [英] How can I execute a native SQL script in JPA/Hibernate?
本文介绍了我如何在JPA / Hibernate中执行原生SQL脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有数据库转储的SQL脚本。我如何使用Hibernate的 EntityManager
来执行它?
I have a SQL script with database dump. How can I execute it using Hibernate's EntityManager
?
我试过这种方式:
I tried it this way:
EntityManager manager = getEntityManager();
Query q = manager.createNativeQuery(sqlScript);
q.executeUpdate();
但它仅在 sqlScript
包含单个SQL查询,而我需要运行多个插入和其他复杂的东西。
but it works only when sqlScript
contains a single SQL query, while I need to run multiple inserts and other complex stuff.
RDBMS:
Oracle Database 11g速成版版本11.2.0.2.0 - 64位生产
RDBMS: Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
推荐答案
用begin end block包装查询。像
Wrap your query with begin end block. Like
EntityManager manager = getEntityManager();
Query q = manager.createNativeQuery("BEGIN " + sqlScript + " END;");
q.executeUpdate();
这篇关于我如何在JPA / Hibernate中执行原生SQL脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文