users@glassfish.java.net

CriteriaQuery and OrderBy

From: <forums_at_java.net>
Date: Mon, 8 Aug 2011 16:51:56 -0500 (CDT)

-------- BASED ON: 

HTTP://BLOGS.ORACLE.COM/ARUNGUPTA/ENTRY/TOTD_148_JPA2_METAMODEL_CLASSES

I am using NB 7.0.1 and working with GF 3.1.1 on Windows XP SP3. The
EclipseLink and EclipseLink-modelgen are present and the metamodel classes
have been generated.
I have worked through following this example and the information given in
http://download.oracle.com/javaee/6/tutorial/doc/gjivm.html [1] . I am trying
to use the CriteriaQuery orderBy. I have established my Root, my Metamodel,
and EntityType. However, the EntityType used (
root.get(EntityType_.&lt;column name&gt;) cannot find the column name at all.
I am using a primary key, but even using a column name that is not part of
the primary key fails. Your example seems simple enough, but the real world
use coughs and chokes. Where do I need to do to find resolution to my issue?

Method (made generic):

    public List<Sydata> findRangeOrdered(int[] range) {
        String methodName = "findAllSortedOrdered";
        logger.log( Level.INFO, "{0}: started", methodName);
        em = getEntityManager();
        Metamodel m = em.getMetamodel();
        EntityType<Sydata> Sydata_ = m.entity(Sydata.class);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery cq = cb.createQuery(Sydata.class);
        TypedQuery<Sydata> q = getEntityManager().createQuery(cq);
        try {
            cq = cb.createQuery(Sydata.class);
            Root<Sydata> sydata = cq.from(Sydata.class);
            cq = cb.createQuery(Sydata.class);
            cq.select(sydata);
-->         cq.orderBy(cb.asc(sydata.get(Sydata_.sydataPK.pdConum),
sydata.get(Sydata_.sydataPK.pdPlnum), sydata.get(Sydata_.sydataPK.pdDesc)));
            TypedQuery<Sydata> tq =
getEntityManager().createQuery(cq);
            tq.setMaxResults(range[1] - range[0]);
            tq.setFirstResult(range[0]);
            q = tq;
            }
        catch( Exception ex)
            {
            logger.log( Level.INFO, methodName + ": Exception", ex);
            }
        finally
            {
            em.clear();
            logger.log( Level.INFO, "{0}: ended", methodName);
            return q.getResultList();
            }
    }
 

Metamodle:

package com.company.persist;
import com.company.persist.SydataPK;
import java.math.BigDecimal;
import javax.annotation.Generated;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;
@Generated(value="EclipseLink-2.2.0.v20110202-r8913",
date="2011-08-08T17:25:20")
@StaticMetamodel(Sydata.class)
public class Sydata_ {
    public static volatile SingularAttribute<Sydata, BigDecimal> pdDbl2;
    public static volatile SingularAttribute<Sydata, BigDecimal> pdDbl3;
    public static volatile SingularAttribute<Sydata, BigDecimal> pdDbl1;
    public static volatile SingularAttribute<Sydata, SypldatPK> sydataPK;
    public static volatile SingularAttribute<Sydata, String> pdLump;
}

Metamodel:

package com.company.persist;
import javax.annotation.Generated;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;
@Generated(value="EclipseLink-2.2.0.v20110202-r8913",
date="2011-08-08T17:25:20")
@StaticMetamodel(SydataPK.class)
public class SydataPK_ {
    public static volatile SingularAttribute<SydataPK, Integer> pdPlnum;
    public static volatile SingularAttribute<SydataPK, String> pdDesc;
    public static volatile SingularAttribute<SydataPK, Integer> pdConum;
}

 


[1] http://download.oracle.com/javaee/6/tutorial/doc/gjivm.html

--
[Message sent by forum member 'rdblaha1']
View Post: http://forums.java.net/node/830841