8

我想从我写过代码relationId的表中获取列的最大值,但它给出了错误ElementRelationType

CriteriaBuilder cb1 = entityManager.getCriteriaBuilder();
CriteriaQuery<ElementRelationTypes> cq1 = cb1.createQuery(ElementRelationTypes.class);
Root<ElementRelationTypes> root = cq1.from(ElementRelationTypes.class);
cq1.select(cb1.max(root.get("relationId")));

选择和最大值都给出错误如何获得整数最大值

public class ElementRelationTypes {

private RelationId relationLangPK=new RelationId(); 

private Country country;
private Status status;


@EmbeddedId
public RelationId getRelationLangPK() {
    return relationLangPK;
}
public void setRelationLangPK(RelationId relationLangPK) {
    this.relationLangPK = relationLangPK;
}

@Transient
public Integer getRelationId() {
    return getRelationLangPK().getRelationId();
}
public void setRelationId(Integer relationId) {
    getRelationLangPK().setRelationId(relationId);
}
@Transient
public Language getLanguage() {
    return getRelationLangPK().getLanguage();
}
public void setLanguageCode(Language language) {
    getRelationLangPK().setLanguage(language);
}

public class RelationId implements Serializable {
private static final long serialVersionUID = 1L;
private Integer relationId;
private Language language;

@JoinColumn(name=PersistenseConstants.ELEMENT_RELATION_TYPE_COL_RELATION_ID)
public Integer getRelationId() {
    return relationId;
}

public void setRelationId(Integer relationId) {
    this.relationId = relationId;
}

@OneToOne
@JoinColumn(name=PersistenseConstants.LANGUAGE_ENTITY_COL_LANG_CODE)
public Language getLanguage() {
    return language;
}

public void setLanguage(Language language) {
    this.language = language;
}
4

1 回答 1

12

您没有发布您收到哪些错误,所以我必须猜测。

CriteriaBuilder.max接受Expression<N>whereN extends Number

同时Root.get默认返回Path<Object>不可转换为Expression<Number>.

因此,要使您的电话max正常工作,您需要将通用参数指定为root.get

cq1.select(cb1.max(root.<Number>get("relationId")));

在这里,您可以Number用实际类型的relationId(LongBigInteger)替换

更新: @perissf解决了您的代码的另一个问题。如果您要选择最大值(数字),您应该将您CriteriaQuery的查询声明为NumberElementRelationTypes

于 2013-09-26T08:26:24.870 回答