【发布时间】:2015-03-13 11:19:06
【问题描述】:
这是我要从中生成查询的根实体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 子句?
提前致谢。
【问题讨论】:
标签: java sql hibernate jpa criteria