如何在jpa中使用复数属性进行条件查询?

yh2wf1be  于 2021-06-30  发布在  Java
关注(0)|答案(4)|浏览(483)

这是我的根实体 ArticleType 我想从中生成一个查询。我想去拿些收藏品 articleTypeVarianteOptionCollection 并为该集合添加一些条件。

public class ArticleType extends BaseEntity implements Serializable
{
    private static final long                   serialVersionUID    = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "art_typ_index")
    private Integer                             artTypIndex;
    @Column(name = "art_typ_code", nullable = false)
    private String                              artTypCode;
    @OneToMany(mappedBy = "atvoIndexArticleType", fetch = FetchType.LAZY)
    private Set<ArticleTypeVarianteOption>      articleTypeVarianteOptionCollection;

    public Integer getArtTypIndex()
    {
        return artTypIndex;
    }

    public void setArtTypIndex(Integer artTypIndex)
    {
        this.artTypIndex = artTypIndex;
    }

    public String getArtTypCode()
    {
        return artTypCode;
    }

    public void setArtTypCode(String artTypCode)
    {
        this.artTypCode = artTypCode;
    }

    @XmlTransient
    public Set<ArticleTypeVarianteOption> getArticleTypeVarianteOptionCollection()
    {
        return articleTypeVarianteOptionCollection;
    }

    public void setArticleTypeVarianteOptionCollection(Set<ArticleTypeVarianteOption> articleTypeVarianteOptionCollection)
    {
        this.articleTypeVarianteOptionCollection = articleTypeVarianteOptionCollection;
    }

}

这是我的 OptionArticle 实体:

public class ArticleTypeOption extends BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id 
      @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ato_index")
    private Integer atoIndex;

    @Column(name = "ato_isremoved")
    private Integer atoIsremoved;
    @JoinColumn(name = "ato_index_art_type", referencedColumnName = "art_typ_index")
    @ManyToOne(fetch = FetchType.LAZY)
    private ArticleType atoIndexArtType;
    @JoinColumn(name = "ato_index_option", referencedColumnName = "opt_art_index")
    @ManyToOne(fetch = FetchType.LAZY)
    private OptionArticle atoIndexOption;

    public ArticleTypeOption() {
    }

    public ArticleTypeOption(Integer atoIndex) {
        this.atoIndex = atoIndex;
    }

    public Integer getAtoIndex() {
        return atoIndex;
    }

    public void setAtoIndex(Integer atoIndex) {
        this.atoIndex = atoIndex;
    }

    public Integer getAtoIsremoved() {
        return atoIsremoved;
    }

    public void setAtoIsremoved(Integer atoIsremoved) {
        this.atoIsremoved = atoIsremoved;
    }

    public ArticleType getAtoIndexArtType() {
        return atoIndexArtType;
    }

    public void setAtoIndexArtType(ArticleType atoIndexArtType) {
        this.atoIndexArtType = atoIndexArtType;
    }

    public OptionArticle getAtoIndexOption() {
        return atoIndexOption;
    }

    public void setAtoIndexOption(OptionArticle atoIndexOption) {
        this.atoIndexOption = atoIndexOption;
    }
}

我的问题是:

SELECT 
articleType
FROM ArticleType articleType 
LEFT JOIN articleType.articleTypeVarianteOptionCollection atOption
where atOption.atoIsremoved = 0;

我试过jpa中的where子句:-

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> criteriaQry = criteriaBuilder.createQuery(entityClass);
Root<T> root = criteriaQry.from(entityClass);
criteriaQry.select(root).distinct(true);

for (PluralAttribute<? super T, ?, ?> pa : root.getModel().getPluralAttributes())
{
    System.out.println(pa.getName());
    System.out.println(pa.getCollectionType());
}

现在如何使用这个pluralattribute添加where子句?
提前谢谢。

aurhwmvo

aurhwmvo1#

添加 where 子句(condition),我必须使用下面指定的连接 joinOptions .
为了检索数据,我必须将这些数据作为获取关系来获取。

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ArticleType> criteria = criteriaBuilder.createQuery(ArticleType.class);
    Root<ArticleType> root = criteria.from(ArticleType.class);
    criteria.select(root).distinct(true);

    Join<ArticleType, ArticleTypeVarianteOption> joinOptions = root.join("articleTypeVarianteOptionCollection");

    if (fetchRelations != null)
    {
        for (String fetchReln : fetchRelations)
        {
            FetchParent<ArticleType, ArticleType> fetch = root;

            for (String reln : fetchReln.split("\\."))
            {
                FetchParent<ArticleType, ArticleType> originalFetch = fetch;
                for (String childReln : reln.split(":"))
                {
                    fetch = originalFetch.fetch(childReln, JoinType.LEFT);
                }
                originalFetch = fetch;
            }
        }
    }

    Predicate[] predArray = new Predicate[2];
    predArray[0] = criteriaBuilder.equal(joinOptions.get("atvoIndexConfig"), configId);
    predArray[1] = criteriaBuilder.equal(joinOptions.get("atvoIndexArticleType"), articleTypeId);

    criteria.where(predArray);

    TypedQuery<ArticleType> typedQry = entityManager.createQuery(criteria);
    ArticleType articleTypeResult;

    try
    {
        articleTypeResult = typedQry.getSingleResult();
    }
    catch (NoResultException ex)
    {
        articleTypeResult = null;
    }
    return articleTypeResult;
o2gm4chl

o2gm4chl2#

使用jpql可以实现您需要的功能。查询类似于条件解决方案,但对我来说更具可读性:

SELECT distinct a FROM ArticleType a 
LEFT JOIN FETCH a.articleTypeViarianteOptionCollection atOption   
WHERE atOption is null OR atOption.atoIsremoved=0
9ceoxa92

9ceoxa923#

首先,让我们从sql查询开始:

SELECT 
articleType
FROM ArticleType articleType 
LEFT JOIN articleType.articleTypeVarianteOptionCollection atOption
where atOption.atoIsremoved = 0;

每当你使用 LEFT JOIN 中的表 WHERE 条件,则联接将表现为 INNER JOIN .
因此,以下是如何将此sql查询转换为条件:

Integer atoIsremoved = ...;

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<ArticleType> criteria = criteriaBuilder.createQuery(ArticleType.class);

Root<ArticleType> root = criteria.from(ArticleType.class);
criteria.select(root).distinct(true);

Join<ArticleType, ArticleTypeVarianteOption> joinOptions = root.join(
    "articleTypeVarianteOptionCollection", 
    JoinType.LEFT
);

criteria.where(
    criteriaBuilder.or(
        criteriaBuilder.isNull(
           joinOptions.get("id")
        ), 
        criteriaBuilder.equal(
            joinOptions.get("atoIsremoved"), atoIsremoved
        )
    )
);

TypedQuery<ArticleType> query = entityManager.createQuery(criteria);
List<ArticleType> resultList = query.getResultList();
wf82jlnq

wf82jlnq4#

您可以使用以下内容:

Fetch artTypeFetch = root.fetch("atoIndexArtType", JoinType.LEFT);
artTypeFetch.fetch("articleTypeVarianteOptionCollection", JoinType.LEFT);

相关问题