如何在Hibernate @ManyToMany关系中添加where子句?

时间:2017-12-06 11:30:49

标签: oracle hibernate many-to-many jointable hibernate-filters

给出两个实体:

  • @Override public String toString() { return voucher_period; }
  • PurchaseProductGroup,其PurchaseProduct列可以是status(有效)或'A'(已删除)

'D'中定义的多对多关系:

PurchaseProductGroup

如何对此进行限制,以便排除状态为/** The purchase products linked to this group. */ @ManyToMany @JoinTable(name = "purchaseprodgrp_purchaseprod", joinColumns = @JoinColumn(name = "ppg_id"), inverseJoinColumns = @JoinColumn(name = "ppr_id")) private List<PurchaseProduct> purchaseProducts; 的buyProducts?

我尝试过的事情

尝试在'D'注释的下方添加以下内容,但两列都失败并且列的“无效标识符”例外:

  1. @ManyToMany
  2. @Where(clause = "status <> 'D'")
  3. 还尝试在实体级别添加@WhereJoinTable(clause = "purchaseProduct.status <> 'D'"),但这似乎不会影响关系集合的内容 - 由ConvLSTM2D备份。

1 个答案:

答案 0 :(得分:2)

请尝试以下代码:

@ManyToMany
@JoinTable(name = "purchaseprodgrp_purchaseprod",
       joinColumns = @JoinColumn(name = "ppg_id"),
       inverseJoinColumns = @JoinColumn(name = "ppr_id"))
@Where(clause = "ppr_status <> 'D'")
private List<PurchaseProduct> purchaseProducts;

这可能类似于您之前尝试过的内容,但实现此功能的关键点是ppr_status是实际的列名。因此PurchaseProduct实体应具有以下内容:

@Column(name="ppr_status")
public String getStatus() {
    return status;
}

如果您已将字段命名为ppr_status,则可能不需要@Column。但根据您的上述评论,我们需要告诉Hibernate如何映射此列。

参考:Hibernate annotations. @Where vs @WhereJoinTable