想从表 ElementRelationType 中获取复合列relationId 的最大值我编写了仅适用于非复合键的代码。我的compositKey 包含relationId 和语言
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);
}
复合类
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)