无法在Hibernate中为oneToMany映射添加限制

时间:2017-03-14 14:56:51

标签: java hibernate hibernate-criteria

我使用oneToMany映射将2个实体链接在一起。在Dao层中,当我对链接实体应用限制时,它会获取所有结果。似乎这些限制不适用于链接实体。我想对两个实体都应用限制。

DAO

Criteria criteria = createEntityCriteria()
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                .add(Restrictions.eq("status" , "APPROVED"))
                .addOrder(Order.desc("approvedAt"))
                .createAlias("purchaseDemandDetails" , "pds")
                .add(Restrictions.ge("pds.approvedQuantity" , 1));
        return criteria.list();

PurchaseDemand.java

@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne
    @JoinColumn(name = "created_by", referencedColumnName = "id")
    private User createdBy;

    @Column(name = "created_at")
    private Date createdAt;

    @ManyToOne
    @JoinColumn(name = "updated_by" , referencedColumnName = "id")
    private User updatedBy;

    @Column(name = "updated_at")
    private Date updatedAt;

    @ManyToOne
    @JoinColumn(name = "approved_by" , referencedColumnName = "id")
    private User approvedBy;

    @Column(name = "approved_at")
    private Date approvedAt;

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

    @OneToMany(mappedBy = "purchaseDemand", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    private Set<PurchaseDemandDetail> purchaseDemandDetails = new HashSet<PurchaseDemandDetail>();
    public void setPurchaseDemandDetails(Set<PurchaseDemandDetail> purchaseDemandDetails)
    {
        this.purchaseDemandDetails.addAll(purchaseDemandDetails);
    }
    public Set<PurchaseDemandDetail> getPurchaseDemandDetails()
    {
        return this.purchaseDemandDetails;
    }

PurchaseDemandDetail.java

  @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne
    @JoinColumn(name = "purchase_demand_id",referencedColumnName = "id")
    @JsonIgnore
    private PurchaseDemand purchaseDemand;

    @ManyToOne
    @JoinColumn(name = "product_id",referencedColumnName = "id")
    private Product product;

    @Column(name = "requested_quantity", nullable = false)
    @NotNull(message = "Quantity is required")
    private int requestedQuantity;

    @Column(name = "approved_quantity", nullable = false)
    @NotNull(message = "Quantity is required")
    private int approvedQuantity;
}

0 个答案:

没有答案