在Spring JPA中基于外键关联更新表中的值

时间:2018-10-20 13:59:01

标签: java spring-boot jpa

我有两个桌子。 表A

@Entity
public class MerchantsInfo implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String firstName;

    private String lastName;
}

表B

@Entity
public class MerchantsSdkConfig implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @JoinColumn( name = "merchantId")
    @ManyToOne(fetch = FetchType.LAZY)
    private MerchantsInfo merchant;


    private String applicationId;
}

现在,我想通过外键applicationId更新表B中的merchantId。我尝试了以下

@Modifying
@Query("update MerchantsSdkConfig u set u.applicationId = ?1 where u.merchantId = ?2")
public int updateApplicationId(String applicationId, Long merchantId);

但是我仍然收到下面的错误

  

无法解析以下属性:com.bus.api.entity.MerchantsSdkConfig的商人ID [更新com.bus.api.entity.MerchantsSdkConfig,请设置u.applicationId =?1,其中u.merchantId =?2]

1 个答案:

答案 0 :(得分:0)

@Modifying
@Query("update MerchantsSdkConfig u set u.applicationId = ?1 where u.merchant.id = ?2")
public int updateApplicationId(String applicationId, Long merchantId);

感谢对问题的评论。我可以通过上述方法解决以下问题。

显然,在进入所需的列之前,我不得不引用/访问映射表。