Java JPA:MapJoin上带谓词的计数

Java JPA:MapJoin上带谓词的计数,java,hibernate,jpa,jpa-2.0,predicate,Java,Hibernate,Jpa,Jpa 2.0,Predicate,我对MapJoin的条件计数查询有问题! 事实上它不起作用 这是我的密码: public long countItems(final String title, final String url) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<CmsItem> query = builder.createQuery(entityClass); Root

我对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;
}
以下是我的实体:

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;
}


}
我不明白为什么我在尝试这样做时会出现这个错误。。。 因为没有计数(在另一种查找项目的方法中),它工作得很好。。。 但是要计算所有搜索结果…请求是错误的

有人能帮我纠正吗


非常感谢

该错误可能是由于com.demkocompany.models.CmsItem类没有linkRewrite属性造成的。仔细检查你是否拥有它,并且无障碍性必须是公开的(我认为)


我添加实体是为了帮助理解问题。我添加实体是因为此方法不在CmsItem上,而是在CmsItemLang上,这就是我使用MapJoin的原因,我将实体添加到我的第一篇文章中,如果您有想法,这将非常好;)
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;
}

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

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