我对使用MapJoin的条件计数查询有疑问! 事实上它不起作用!
这是我的代码:
public long countItems(final String title, final String url) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<CmsItem> query = builder.createQuery(entityClass);
Root<CmsItem> page = query.from(entityClass);
query.select(page);
MapJoin<Map<Lang, CmsItemLang>, Lang, CmsItemLang> mapJoin = page
.joinMap("cmsItemLang");
List<Predicate> predicateList = new ArrayList<Predicate>();
Predicate titlePredicate, urlPredicate;
if ((title != null) && (!(title.isEmpty()))) {
titlePredicate = builder.like(
builder.upper(mapJoin.value().<String> get("metaTitle")),
"%" + title.toUpperCase() + "%");
predicateList.add(titlePredicate);
}
if ((url != null) && (!(url.isEmpty()))) {
urlPredicate = builder.like(
builder.upper(mapJoin.value().<String> get("linkRewrite")),
"%" + url.toUpperCase() + "%");
predicateList.add(urlPredicate);
}
Predicate[] predicates = new Predicate[predicateList.size()];
predicateList.toArray(predicates);
query.where(predicates).distinct(true);
CriteriaQuery<Long> cq = builder.createQuery(Long.class);
cq.select(builder.count(cq.from(entityClass)));
entityManager.createQuery(cq);
cq.where(predicates);
Long count = entityManager.createQuery(cq).getSingleResult();
return count;
}
当我调用方法url param或title param不为null时,我有这个错误:
org.hibernate.QueryException: could not resolve property: linkRewrite of: com.demkocompany.models.CmsItem [select count(*) from com.demkocompany.models.CmsItem as generatedAlias0 where upper(generatedAlias0.linkRewrite) like :param0]
这是我的实体:
public class CmsItem {
@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.REMOVE }, fetch = FetchType.EAGER, mappedBy = "cmsItemLangPK.item")
@MapKey(name = "cmsItemLangPK.lang")
private Map<Lang, CmsItemLang> cmsItemLang;
public CmsItem() {
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Map<Lang, CmsItemLang> getCmsItemLang() {
return cmsItemLang;
}
public void setCmsItemLang(Map<Lang, CmsItemLang> cmsItemLang) {
this.cmsItemLang = cmsItemLang;
}
}
和第二个实体(对于地图)
public class CmsItemLang implements Serializable {
private static final long serialVersionUID = 6832580916240288447L;
@EmbeddedId
private CmsItemLangPK cmsItemLangPK;
@Column(name = "title")
private String title;
@Column(name = "description")
private String description;
@Lob
@Column(name = "text")
private String text;
@Column(name = "linkRewrite")
private String linkRewrite;
@Column(name = "meta_title", length = 128)
private String metaTitle;
@Column(name = "meta_keywords", length = 255)
private String metaKeywords;
@Column(name = "meta_description", length = 255)
private String metaDescription;
public CmsItemLang() {
}
public CmsItemLangPK getCmsItemLangPK() {
return cmsItemLangPK;
}
public void setCmsItemLangPK(CmsItemLangPK cmsItemLangPK) {
this.cmsItemLangPK = cmsItemLangPK;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getLinkRewrite() {
return linkRewrite;
}
public void setLinkRewrite(String linkRewrite) {
this.linkRewrite = linkRewrite;
}
public String getMetaTitle() {
return metaTitle;
}
public void setMetaTitle(String meta_title) {
this.metaTitle = meta_title;
}
public String getMetaKeywords() {
return metaKeywords;
}
public void setMetaKeywords(String meta_keywords) {
this.metaKeywords = meta_keywords;
}
public String getMetaDescription() {
return metaDescription;
}
public void setMetaDescription(String meta_description) {
this.metaDescription = meta_description;
}
}
当我尝试这样做时,我不明白为什么会出现这个错误... 因为没有计数(在其他方法中找到项目)它运作良好... 但要计算搜索的所有结果......请求是错误的......
有人可以帮我纠正这个问题吗?
非常感谢
答案 0 :(得分:0)
该错误可能是由于类com.demkocompany.models.CmsItem没有linkRewrite属性。仔细检查你是否拥有它,并且可访问性必须公开(我认为)
public String getLinkRewrite() {
// ...
}
public void setLinkRewrite(String linkRewrite) {
// ...
}