spring数据jpa规范:过滤复杂数据

kiz8lqtg  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(213)

我试图从下表中筛选一些数据。我已经使用本机查询成功地完成了这项工作。但是有些需求是如何改变的,现在我想用jpa标准规范来做,因为我对jpa规范非常陌生,我发现很难找到解决方案。非常感谢您的帮助。
idaccount\u idcodeassociated\u idPercentage1A608Qprnull1002A609BU00Qnull1003A6106000RNnull1004A611BU00P17405A611BU00PNULL1006A609BU00Q17607A611BU00P20708A610BU00R1760
我试图实现的是,当我提供associate\u id作为输入时,我需要以100%的百分比显示它使用的所有account\u id行以及未使用的行。
例如:如果我提供关联的\u id为17..它必须显示id为4、6、8和1的条目(因为它是未使用的,并且具有100%的值)。我已经使用query实现了这一点
查询
选择from(selectfrom account\U entities2 where associated\U id=17 union select*from account\U entities2 where account\U id not in(select account\U id from account\U entities2 where associated\U id=17)和PERCENTATION=100)e order by account\U id;
输出:
idaccount\u idcodeassociated\u idpercentage1a608qprnull1006a609bu00q17608a610bu00r17604a611bu00p1740
对于关联的\u id=20,输出将是(因为只有一个帐户\u id被使用,其余的未使用,如下所示)
idaccount\u idcodeassociated\u IDPercentage1A608Qprnull1002A609BU00Qnull1003A6100BU00RNnull1007A611BU00P2070
如前所述,查询工作正常,但…我需要使用SpringDataJPA规范来实现相同的结果,到目前为止我所拥有的是。。

final Specification<AccountRecord> spec = criteria.getSpecification()
                .and((root, criteriaQuery, criteriaBuilder) ->
                        criteriaBuilder.equal(root.get("associated_id"), associateId))
                .or((root, criteriaQuery, criteriaBuilder) ->
                     root.get("associated_id").isNull())
                .and((root, criteriaQuery, criteriaBuilder) ->
                        criteriaBuilder.greaterThan(root.get("percentage"), 0));

public class AccountRecord {

    @Id
    @Column(name="id")
    private String id;

    @Column(name = "account_id", nullable = false)
    protected String account_id;

    @Column(name = "code", nullable = false)
    private String code;

    @Column(name = "associated_id", nullable = false)
    private Integer associated_id;

    @Column(name = "percentage", nullable = false)
    private BigDecimal percentage;
}

我很天真ï我们已经了解了jpa规范,非常感谢您的帮助,谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题