- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有下一个实体
@Entity
@Table(name = "account")
public class Account {
@Id
private long id;
private long amount;
}
我想为现有值添加一些值并在数据库中更新它或者,如果此帐户尚不存在,我想创建一个新帐户。
Spring Data JPA 的基本方法是:
var account = dataJpaRepository.findById(id);
if (account==null) {account = new Account(id,someValue}
else {account.set(account.getAmount() + someValue);}
dataJpaRepository.save(account);
在这种情况下,完成了对数据库的两个请求,但是使用 native 查询我可以通过发送一个请求来完成,例如:
"INSERT INTO account (id, amount) VALUES (:id, someValue) ON DUPLICATE KEY UPDATE
amount = account.amount + someValue"
是否可以通过 Spring Data JPA/JPQL 来做同样的事情并减少请求量?
最佳答案
绝对不确定这是否有效,但您可以尝试两件事:
@Repository
public interface AccountRepo extends JpaRepository<Account, Long> {
// First try
@Query(native = "INSERT INTO account (id, amount) VALUES (:id, :someValue) ON DUPLICATE KEY UPDATE amount = account.amount + :someValue")
Long addAmount(@Param("id") Long id, @Param("someValue") Long amount);
// Second try
default Account addAmount(Long id, Long amount) {
findById(id).map(a -> {a.amount += amount; return a;}).ifPresentOrElse(a -> save(a), () -> {return save(new Account(amount));})
}
}
关于java - Spring 数据 JPA "ON DUPLICATE KEY UPDATE amount = account.amount + someValue",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69373529/
下面两个说法是否相似? if (isset($someValue) && $someValue != '') 和 if(!empty($someValue])) 最佳答案 不,它们不一样。是的,它们很
你好,我的问题可能有点愚蠢,但如果有人能帮助我,我将不胜感激。 所以我的问题是:如何使用 ninject 设置自动类与接口(interface)比较规则 I+"Class"= "Class"? 让我们
这个问题在这里已经有了答案: What is the difference between getter-only auto properties and expression body prope
当我创建以下 constructor 时,我收到错误信息 Default value required for 'DefaultPort' type MyIdServer = class pu
我在 XML 代码中多次使用这个: ${some.value} some.value 可能来自哪里 属性文件 System.setProperty("some.value", "any_value")
面试面试看到如下代码: 编辑: public class TestValue { private Value defaultValue; @Test public void E
where T : somevalue 是什么意思?我刚刚看到一些代码说 where T : Attribute。我认为这与泛型有关,但我不确定这意味着什么或它在做什么。 有人知道吗? 最佳答案 这是
我正在尝试将一些 VB.net 代码转换为 C#。我使用 SharpDevelop 来完成繁重的工作;但它生成的代码在某些枚举操作上出现问题,我不确定如何手动修复它。 原始 VB.net 代码: En
Mysql 只允许执行“select 1”并将返回“1”的列/值。另外,如果我提供一些随机字符串。 mysql> select "foo" ; +-----+ | foo | +-----+ | fo
function giveValue(n){ ["r"+n]=5; } giveValue(10); 你明白了。 关键是我有一些名称相似的变量,仅末尾的数字有所不同。使用 switch 语句几
我一直在尝试向 HTML 输入中插入一个值,如下所示: $("#someID").val($("#anotherID").html()); 问题是 $("#anotherID").html() 比默认
我有一个文本文件(实际上是 .xml),其中包含 MACAddress=SOMEVALUE 形式的某些行,并混合在其他行中。例如,典型的此类行是(在单行上): 注意在上面的行中,SOMEVALUE
a)在这种情况下,随机数生成器是否在每次运行时都使用系统时钟(改变种子)? b) 种子是否用于生成 expovariate(lambda) 的伪随机值? 最佳答案 “使用源头,卢克!”...;-)。学
如何在 lldb 中设置断点,当 register == somevalue 时停止(rdi = 0x0000000000000000)?可能吗? 最佳答案 lldb 表达式解析器提供$rdi、$rs
if((isset($_GET[example]))&&($_GET['example']=='somevalue')){ ... } 或 if((!empty($_GET[example]))&&(
我有一个自定义类的列表(它具有名称、年龄、地址等属性)。我如何检查列表中是否存在“名称”值为“shyju”的项目,如果存在则返回它它存在。名称将是唯一的。没有两个项目具有相同的名称。 我现在想到的解决
我只是在大学里学习 C++。我试图从 GitHub 编译代码来解决 Rubiks 魔方。我在以下代码行中遇到错误: Table = int (*)[Cube::Move::NumberOfCloc
每当我尝试将具有此架构的文档保存到我的数据库中时,我都会遇到错误: var schemaForBooks = new Schema({ book: String, author: St
我正在尝试创建一个需要 4 个参数的存储过程。这 4 个参数决定了返回表的外观。 @C_ID 参数始终为数字。 @U_ID 和 @P_ID 值可以包含有效的数值,或者可以(或应该)作为 NULL 传递
redis> SMEMBERS CO:1:A 1) "1" 2) "2" redis> SMEMBERS CO:2:A 1) "1" 2) "5" 3)
我是一名优秀的程序员,十分优秀!