使用JPA Criteria API返回Collection属性包含特定值的所有对象

时间:2017-01-23 10:53:40

标签: java spring jpa spring-data-jpa criteria-api

我正在尝试为我的Spring-data-jpa项目动态创建Specification

这是我目前的规范:

public class DemandsSpecs implements Specification<SvDem> {

    private static final Logger LOG = LogManager.getLogger();

    private final DemandsCritera critera;
    private final List<Predicate> predicateList;

    public DemandsSpecs(final DemandsCritera critera) {
        this.critera = critera;
        this.predicateList = new ArrayList<>();
    }

    @Override
    public Predicate toPredicate(final Root<SvDem> root,
                                 final CriteriaQuery<?> query,
                                 final CriteriaBuilder cb) {

        this.predicateList.add(cb.between(root.get(SvDem_.hdCreation), critera.getBegin(), critera.getEnd()));

        if (critera.getSoc() != null) {
            LOG.debug("socId {}", critera.getSoc());
            this.predicateList.add(cb.equal(root.get(SvDem_.socId), critera.getSoc()));
        }

        if (critera.getManagementAct() != null) {
            LOG.debug("actgesId {}", critera.getManagementAct());
            this.predicateList.add(cb.equal(root.get(SvDem_.actgesId), critera.getManagementAct()));
        }

        if (critera.getStatus() != null) {
            LOG.debug("statutId {}", critera.getStatus());
            this.predicateList.add(cb.equal(root.get(SvDem_.statutId), critera.getStatus()));
        }

        if (!StringUtils.isBlank(critera.getId())) {
            LOG.debug("id {}", critera.getId());
            this.predicateList.add(cb.like(root.get(SvDem_.id), '%' + critera.getId() + '%'));
        }

        return query.where(cb.and(predicateList.toArray(new Predicate[predicateList.size()])))
                .orderBy(cb.desc(root.get("hdCreation")))
                .getRestriction();
    }
}

DemandsCritera也有String属性metadata

public class DemandsCritera implements Serializable {

    private static final Long serialVersionUID = 1L;

    private Date begin;
    private Date end;
    private RfActges managementAct;
    private String metadata;
    private RfStatut status;
    private RfSoc soc;
    private String id;

    /* getters and setters */
}

SvDem的属性svMetaCollectionCollection<SvMeta>

@Entity
@Table(name = "sv_dem")
public class SvDem implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Basic(optional = false)
    private String id;
    @Basic(optional = false)
    @Column(name = "HD_CREATION")
    @Temporal(TemporalType.TIMESTAMP)
    private Date hdCreation;

    @Lob
    private String user;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "svDem")
    @JsonManagedReference
    private Collection<SvMeta> svMetaCollection;

    @JoinColumn(name = "ACTGES_ID", referencedColumnName = "ID")
    @ManyToOne(optional = false)
    private RfActges actgesId;

    @JoinColumn(name = "STATUT_ID", referencedColumnName = "ID")
    @ManyToOne(optional = false)
    private RfStatut statutId;

    @JoinColumn(name = "SOC_ID", referencedColumnName = "ID")
    @ManyToOne(optional = false)
    private RfSoc socId;

    /* getters, setters and other irrelevant properties */

}

SvMeta对象具有String属性value

@Entity
@Table(name = "sv_meta")
public class SvMeta implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected SvMetaPK svMetaPK;

    @Basic(optional = false)
    @Lob
    private String value;

    @JoinColumn(name = "META_ID", referencedColumnName = "ID", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private RfMeta rfMeta;

    @JoinColumn(name = "DEM_ID", referencedColumnName = "ID", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    @JsonBackReference
    private SvDem svDem;
}

我想返回SvDemSvMeta的任何value等于DemandsCritera.metadata的所有function safeFocus() { $timeout(function () { element[0].focus(); }, 0); }

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

我只需将其添加到{% for key,value in arr if key == 'a' and value == 'Test'%} <div> <label>{{ key }}</label> <span>{{ value }}</span> </div> {% endfor %} 的{​​{1}}方法:

toPredicate