Nav
Nav

Reputation: 4540

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

Upvotes: 4

Views: 463

Answers (1)

JB Nizet
JB Nizet

Reputation: 691685

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.

Upvotes: 4

Related Questions