简体   繁体   中英

How to execute DELETE and SELECT statements as one query with JPA?

I would like to get SELECT statement result before DELETE statement is executed, but they both use the same temporary table created at the beginning and I cannot use query.getResultList() and query.executeUpdate() at the same time. Is there any other way?

CREATE TEMPORARY TABLE xyzToDelete SELECT xyz FROM ...
----
SELECT * FROM xyzToDelete
----
DELETE FROM <using xyzToDelete>
private String deleteXYZ;
----
<here I put sql file content into this string>
----
Query query = em.createNativeQuery(deleteXYZ);
query.executeUpdate();

The problem with ACID. A transaction will rollback if not completed. You should split your SELECT & DELETE into two transactions.

your best bet would be:

Query query = em.createNativeQuery(CREATE TABLE xyzToDelete SELECT xyz FROM SELECT * FROM xyzToDelete);
List<String> results = query.getResultlist();
Query query2 = em.createNativeQuery(DELETE FROM xyzToDelete);
List<String> changes = query.executeUpdate();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM