0
@Entity
public class TransactionInfo {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @OneToOne
    @JoinColumn(name="txnRequestId")
    private TransactionRequest txnRequest;

    ..
}

我想使用 txnRequestId id 字段(与 TransactionRequest 表的 id 字段映射)对此表进行批量更新

到目前为止,由于我对 HQL 的了解有限,我尝试使用 tr.txnRequest.id 字段来比较 TransactionRequest 表的 id -

@Repository
public interface TransactionInfoRepository extends JpaRepository<TransactionInfo, Long>, JpaSpecificationExecutor<TransactionInfo> {
    

@Transactional
@Modifying
@Query("UPDATE TransactionInfo as tr SET status=?2 WHERE tr.status=?1 and tr.txnRequest.id in (?3)")
int updateStatusToRequestedByTxnReqId(Enum.TransactionStatus oldStatus, Enum.TransactionStatus newStatus, List<Long> trIds);

例外 -

Unexpected errororg.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [UPDATE com.project.model.TransactionInfo as tr SET status=?2 WHERE tr.status=?1 and tr.txnRequest.id in (?3)]; nested exception is java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for 
DML operations [UPDATE com.project.model.TransactionInfo as tr SET status=?2 WHERE tr.status=?1 and tr.txnRequest.id in (?3)]
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [UPDATE com.project.model.TransactionInfo as tr SET status=?2 WHERE tr.stat
us=?1 and tr.txnRequest.id in (?3)]; nested exception is java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [UPDATE com.project.model.TransactionInfo as 
tr SET status=?2 WHERE tr.status=?1 and tr.txnRequest.id in (?3)]

我也尝试过 tr.txnRequestId 但得到了 -

 org.hibernate.QueryException: could not resolve property: txnRequestId of: com.project.model.TransactionInfo

我会很感激 Hibernate 中本章的文档链接或链接,因为我还无法研究这些东西,因此不知道要搜索什么。

4

1 回答 1

0

您可以使用本机查询,然后您可以使用txnRequestId

@Query("UPDATE TransactionInfo SET status=?2 WHERE status=?1 and txnRequestId in (?3)", nativeQuery = true)
int updateStatusToRequestedByTxnReqId(Enum.TransactionStatus oldStatus, Enum.TransactionStatus newStatus, List<Long> trIds);

假设您的数据库表名是TransactionInfo

于 2020-07-09T19:15:00.283 回答