Search code examples
springspring-bootjpajpql

JPQL Insert by selecting query not working


I have two below entity. One is MatchTable another is MatchLog.

@Entity
public class MatchTable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @ManyToOne
    private Integer primaryID;

    @ManyToOne
    private Integer suspectedID;

    @Column(nullable=false)
    private Integer status;

    //getter and setter

}



@Entity
public class MatchLog {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @OneToOne
    private MatchTable referenceID;

    @Column(nullable=false)
    private Long primaryID;

    @Column(nullable=false)
    private Long suspectedID;

    @Column(nullable=false)
    private Integer status;

    //getter and setter
}

If status of MatchTable change,these row will be inserted into MatchLog. I have tried with the below JPQL query.

@Query("INSERT INTO MatchLog (referenceID.id,primaryID,suspectedID,status) SELECT id,primaryID,suspectedID,status from MatchTable where (primaryID = :ID or suspectedID = :ID)")
int updateMatchLogTable(@Param("ID") long ID);

But this JPQL query is not working. Please suggest me what will be the JPQL query to insert change rows from MatchTable to MatchLog.


Solution

  • Unless I'm mistaken, JPA does not support "insert into select". You can change as native query.