简体   繁体   English

我的Native查询和JPQL(JPA)的结果差异

[英]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) 原生查询的结果是:deck = 1和preDeck = 0(这是我想要的)

But in JPQL it is not happend: 但在JPQL中它不会发生:

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

The result is: deck=1 and predeck=1 too 结果是:deck = 1和predeck = 1

What is the solution? 解决办法是什么?

RGDS RGDS

Interesting question. 有趣的问题。 What's the generated SQL for your JPQL query? 为JPQL查询生成的SQL是什么? Which JPA engine do you use? 你使用哪种JPA引擎?

Anyway, the solution is to avoid update queries when using JPA, especially if the number of entities to update is small. 无论如何,解决方案是在使用JPA时避免更新查询,尤其是在要更新的实体数量很少的情况下。 Query for the entities, update their state, and let JPA flush the changes for you. 查询实体,更新其状态,让JPA为您刷新更改。

If a big number of entities must be updated, you could still use a native query rather than a JPQL query. 如果必须更新大量实体,您仍然可以使用本机查询而不是JPQL查询。 Or you could use two queries: one which sets preDeck, and one which set deck to preDeck + 1. 或者您可以使用两个查询:一个设置preDeck,另一个设置deck到preDeck + 1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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