Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
968 views
in Technique[技术] by (71.8m points)

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

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();

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 Express Edition Release 11.2.0.2.0 - 64bit Production

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Wrap your query with begin end block. Like

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...