2013-07-13 50 views
0

我遇到一個使用MapJoin的條件計數查詢問題! 實際上它不起作用!JPA:與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; 
} 

和我有這樣的錯誤,當我調用該方法網址PARAM或標題參數是不是空:

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

該錯誤可能是由於該類com.demkocompany.models.CmsItem沒有linkRewrite財產。仔細檢查你是否擁有它,並且輔助功能必須公開(我認爲)

public String getLinkRewrite() { 
    // ... 
    } 

    public void setLinkRewrite(String linkRewrite) { 
    // ... 
    } 
+0

我添加了實體以幫助理解問題。 – Garthos

+0

我添加了實體,因爲這個方法不在CmsItem上,而是在CmsItemLang上,這就是爲什麼我使用了MapJoin,我將這些實體添加到了我的第一篇文章中,如果你有它並且想法會非常好;) – Garthos