简体   繁体   中英

Difference in result of my Native query and JPQL(JPA)

my native query is (this is work):

Update XXX SET preDeck=deck, deck=deck+1 WHERE ...

the result of native query is: deck=1 and preDeck=0 (it is what I want)

But in JPQL it is not happend:

UPDATE XXX SET preDeck=deck, deck=deck+1 WHERE ...

The result is: deck=1 and predeck=1 too

What is the solution?

RGDS

Interesting question. What's the generated SQL for your JPQL query? Which JPA engine do you use?

Anyway, the solution is to avoid update queries when using JPA, especially if the number of entities to update is small. Query for the entities, update their state, and let JPA flush the changes for you.

If a big number of entities must be updated, you could still use a native query rather than a JPQL query. Or you could use two queries: one which sets preDeck, and one which set deck to preDeck + 1.

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