Question

want to get the maximum value of composite column relationId from table ElementRelationType I have written code which is only working for non composite key. my compositKey contains relationId and 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);

my classes are

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

exception

 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)
Was it helpful?

Solution

It seems to me that the problem is caused by composite expression. Try to access this property in the following way:

cq1.select(
    cb1.max(
        root.<ElementRelationTypes>get("relationLangPK").<Integer>get("relationId")
    )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top