mysql - 如何在 JPA 中使用一个查询更新 3 个表?

标签 mysql jpa spring-data

我有 3 个表 UserDetail、RequestorDetail 和 AddressDetail。 UserDetail 具有 userId(PK)、userName,AddressDetail 具有城市和郊区,RequestorDetail 具有 reqEmail 和 reqPhone。

我想使用一个查询更新所有这些详细信息。截至目前,我正在使用如下 3 个不同的查询。

@Modifying
    @Query("update UserDetail u set "u.userName = :userName, u.password = :password where u.userEmailId = :userEmailId")
    int saveUserDetailData(@Param("userName") String userName, @Param("password") String password,  @Param("userEmailId") String userEmailId);

@Modifying
    @Query("update RequestorDetail r set r.requestorEmailid = :requestorEmailid, r.requestorPhone = :requestorPhone where r.userId = :userId")
    int saveRequestorDetailData(@Param("requestorEmailid") String requestorEmailid,
            @Param("requestorPhone") String requestorPhone, @Param("userId") int userId);

@Modifying
    @Query("update AddressDetail a set a.city = :city, a.suburb = :suburb where a.userId = :userId")
    int saveAddressDetailData(@Param("city") String city, @Param("suburb") String suburb,
            @Param("userId") int userId);

谁能告诉我如何在单个查询中使用所有这些?

最佳答案

不可能使用单个 SQL 查询更新多个表。 请参阅https://community.oracle.com/thread/2225393

您必须将其拆分为不同的查询。

关于mysql - 如何在 JPA 中使用一个查询更新 3 个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37594131/

相关文章:

java - 与 Spring 和 JPA 的事务

java - Spring 数据 JPA : Named method without JpaRepository

postgresql - Spring 数据错误 : "HHH000389 Unsuccessful: drop table if exists"

php - 无法登录 phpmyadmin

php - 从具有相同名称但不同表的两列中检索数据

hibernate - 通过单列而不是复合主键连接

spring-boot - 使用源过滤获取Spring Data Elasticsearch Query中的所有非完整字段

php - 为什么当我查找某些列而不是其他列时,php 返回 json?

Jquery、ajax发布并获取结果?

java - 在 hibernate 中使用复合主键保留 1-m 实体