Failing to perform an UPDATE with JOIN in Spring + JPA

LucaAtWork :

I'm trying to perform an UPDATE using a project with: - Jdk 11 - Springboot 2.2.3 - Mysql 8.0.18

This is my query (in repository):

    @Transactional
    @Modifying
    @Query("UPDATE VC SET bulkId = :bulkId WHERE vId = :vId AND user.ttt=:TTT")
    void updateBulkId(String bulkId, String vId, String TTT);

This is my VC model:

@Entity
@Table
@Getter @Setter
public class VC
{
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name="user_fk", referencedColumnName = "id")
    private User user;

    @Column(nullable = false)
    private String bulkId;

    private String vId;

}

and this is my User model:

@Entity
@Table
@Getter @Setter
public class User
{
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String ttt;
}

This is the error I get:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set bulkId='123ABC' where vId='1' and ttt='123'' at line 1

I think that for some reason JPA is failing to join the two tables (VC and User).

Thanks for the help

NikNik :

You can try with a subquery:

@Query("UPDATE VC SET bulkId = :bulkId WHERE vId = :vId 
  AND user.id in (
    select u.id
    from User u
    where u.ttt=:TTT"
  )
)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=416623&siteId=1