8

I want to get the maximum value of column relationId from table ElementRelationType I have written code but its giving error

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")));

select and max both giving error how to get the integer max value

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

and

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;
}
default locale
  • 13,035
  • 13
  • 56
  • 62
Surya
  • 416
  • 4
  • 7
  • 26

1 Answers1

12

You didn't post which errors do you receive, so I have to guess.

CriteriaBuilder.max accepts Expression<N> where N extends Number

At the same time Root.get by default returns Path<Object> which is inconvertible to Expression<Number>.

So to make your call to max work you need to specify generic parameter to root.get:

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

here you can replace Number with an actual type of relationId (Long, BigInteger etc.)

UPDATE: @perissf addressed another issue with your code. If you are going to select maximal value (which is numeric) you should declare your CriteriaQuery as a query to Number not ElementRelationTypes

Community
  • 1
  • 1
default locale
  • 13,035
  • 13
  • 56
  • 62
  • Its working for non composit key,but in my case relationId is composit key.I have provided my class info – Surya Sep 26 '13 at 09:26
  • @perissf I'm sorry, I just tried to mention that your answer highlights *another* useful point. I'm not sure how it is better, but it is certainly different. Address OP if you think that your answer deserves to be accepted. I already voted up your answer anyway. – default locale Sep 26 '13 at 09:30
  • No problem. Just deleted my answer when new information arrived! – perissf Sep 26 '13 at 09:33
  • @Surya It isn't clear what's the root cause of the error here. Maybe you should consider to post another question. Don't forget to include what exactly doesn't work, compiler errors (if any), exception messages and stacktraces (if any) – default locale Sep 26 '13 at 09:35
  • @perissf Why? Your answer was absolutely relevant to original question. – default locale Sep 26 '13 at 09:36
  • Because I was assuming that `relationId` was a Numeric type. It's not the case, although you are right in saying that it was not possible to have this information at the time of answering. – perissf Sep 26 '13 at 09:41
  • 1
    @perissf Not sure: `public Integer getRelationId()`. I believe it's still Numeric – default locale Sep 26 '13 at 09:45