希望从表中获取复合列relationId的最大值ElementRelationType我已经编写了仅适用于非复合键的代码。我的compositKey包含relationId和language
CriteriaBuilder cb1 = entityManager.getCriteriaBuilder();
CriteriaQuery<Integer> cq1 = cb1.createQuery(Integer.class);
Root<ElementRelationTypes> root = cq1.from(ElementRelationTypes.class);
cq1.select(cb1.max(root.<Integer>get("relationId")));
TypedQuery<Integer> qr = entityManager.createQuery(cq1);
Integer i=qr.getResultList().get(0);
我的课程是
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);
}
compositClass
public class RelationId implements Serializable {
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;
}
例外
java.lang.IllegalArgumentException: Unable to resolve attribute [relationLangPK.relationId] against path
16:14:16,757 ERROR [stderr] (http-/0.0.0.0:8080-1) at org.hibernate.ejb.criteria.path.AbstractPathImpl.unknownAttribute(AbstractPathImpl.java:118)
16:14:16,757 ERROR [stderr] (http-/0.0.0.0:8080-1) at org.hibernate.ejb.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:223)
16:14:16,757 ERROR [stderr] (http-/0.0.0.0:8080-1) at org.hibernate.ejb.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:194)
答案 0 :(得分:1)
在我看来,问题是由复合表达引起的。尝试以下列方式访问此属性:
cq1.select(
cb1.max(
root.<ElementRelationTypes>get("relationLangPK").<Integer>get("relationId")
)
);