具有连接条件的HQL更新

时间:2018-08-12 12:40:11

标签: hibernate join hql

我在休眠状态下的实体之间有关系,

实体 ProgramCardEntity

@Entity
@Table(name = "ProgramCardEntity")
public class ProgramCardEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ParentCardRef", referencedColumnName = "id")
    private VSCardEntity parentCardEntity;

    @Column(name = "Status")
    private String status;

}

实体 VSCardEntity

@Entity
@Table(name = "VSCardEntity")
public class VSCardEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "CardNumber")
    private String cardNumber;
}

我想根据给定的 VSCardEntity 卡号更新 ProgramCardEntity 的状态。

我正在尝试使用HQL查询:

        String query = "update ProgramCardEntity pc inner join pc.parentCardEntity as progCard set pc.status= :status where progCard.cardNumber = :cardNo";

        Query qry = this.sessionFactory.getCurrentSession().createQuery(query);
        qry.setParameter("status", "Issued");
        qry.setParameter("cardNo", "cardNo");

        int updated = qry.executeUpdate();

这会导致错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: expecting "set", found 'inner' near line 1, column 45 [update ProgramCardEntity pc inner join pc.parentCardEntity as progCard set pc.status= :status where progCard.cardNumber = :cardNo]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
    at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)

2 个答案:

答案 0 :(得分:0)

您应该使用子查询而不是联接,如下所示:

"update ProgramCardEntity pc set pc.status = :status where pc.id in
(select progCard.id from pc.parentCardEntity as progCard where
progCard.cardNumber = :cardNo)"

答案 1 :(得分:0)

尝试此查询可能有效:

update ProgramCardEntity  pce set pce.status=: status where pce.parentCardEntity.cardNumber=: cardNumber