admin管理员组文章数量:1391977
I'm struggling to write a rather complex JPQ query, I'm really not sure how the syntax goes for this. Is it MySQL or SQLServer syntax? Here is what I have (censored):
UPDATE FirstTable a INNER JOIN SecondTable b on a.secondTableId = b.secondTableId
SET a.firstItem = ?1 a.secondItem = ?2 b.firstItem = ?3 b.secondItem = ?4
b.thirdItem = ?5 b.fourthItem = ?6 a.thirdItem = ?7 a.fourthItem =?8
WHERE a.ID = ?9 AND a.environment = ?10
The variables are laid out underneath but the part I need help with is just the syntax.
Thanks in advance.
I'm struggling to write a rather complex JPQ query, I'm really not sure how the syntax goes for this. Is it MySQL or SQLServer syntax? Here is what I have (censored):
UPDATE FirstTable a INNER JOIN SecondTable b on a.secondTableId = b.secondTableId
SET a.firstItem = ?1 a.secondItem = ?2 b.firstItem = ?3 b.secondItem = ?4
b.thirdItem = ?5 b.fourthItem = ?6 a.thirdItem = ?7 a.fourthItem =?8
WHERE a.ID = ?9 AND a.environment = ?10
The variables are laid out underneath but the part I need help with is just the syntax.
Thanks in advance.
Share Improve this question asked Mar 13 at 21:37 No longer in collegeNo longer in college 376 bronze badges 1- Can you provide more code how do u plan to use the query? – Nico S. Commented Mar 14 at 0:20
1 Answer
Reset to default 0JPQL does not support JOIN
in UPDATE
queries. This is because JPQL allows to update only a single entity at a time. So the there is no problem with your syntax, such query is simply impossible withing JPQL.
In spite of that you could split the query into two separate methods in your repository class. For example
@Modifying
@Query("UPDATE FirstTable a SET a.firstItem = ?1, a.secondItem = ?2, a.thirdItem = ?7, a.fourthItem = ?8 WHERE a.id = ?9 AND a.environment = ?10")
void updateFirstTable(String firstItem, String secondItem, String thirdItem, String fourthItem, Long id, String environment);
@Modifying
@Query("UPDATE SecondTable b SET b.firstItem = ?3, b.secondItem = ?4, b.thirdItem = ?5, b.fourthItem = ?6 WHERE b.secondTableId = (SELECT a.secondTableId FROM FirstTable a WHERE a.id = ?9 AND a.environment = ?10)")
void updateSecondTable(String firstItem, String secondItem, String thirdItem, String fourthItem, Long id, String environment);
After that call the two methods in Service
class within the same transaction and be done.
@Transactional
public void updateTables(String firstItem, String secondItem, String thirdItem, String fourthItem,
String firstItemB, String secondItemB, String thirdItemB, String fourthItemB,
Long id, String environment) {
repository.updateFirstTable(firstItem, secondItem, thirdItem, fourthItem, id, environment);
repository.updateSecondTable(firstItemB, secondItemB, thirdItemB, fourthItemB, id, environment);
}
You didn't provide any context of the query point and usage but in my opinion it's better to split the query into two as it's more likely that there will be a situation where you need to update the first table while preserving the values of the first one. If such situation is impossible then there might be some design issues.
本文标签: javaJPA update query with inner joinStack Overflow
版权声明:本文标题:java - JPA update query with inner join - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744684166a2619595.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论