Home » Support » Forum » Problem with Criteria Querys

Problem with Criteria Querys

#1

Hi,

i am having problems running following code...

 

import java.util.*;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;


public final class MyTestCase {

    public static void main(String[] args)  {
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory("objectdb:$objectdb/db/test.tmp;drop");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        for (double i=0.0; i<10.0;i++){
         em.persist(new DataObject(i));
        }
        em.getTransaction().commit();

        CriteriaBuilder cb = em.getCriteriaBuilder();
  CriteriaQuery<DataObject> q = cb.createQuery(DataObject.class);
  Root<DataObject> d  = q.from(DataObject.class);
  q.select(d);
  ParameterExpression<Double> p1 = cb.parameter(Double.class);
  ParameterExpression<Double> p2 = cb.parameter(Double.class);
  q.where(
   cb.and(
    cb.gt(d.get("lon"),p1),
    cb.lt(d.get("lon"),p2)
   )
  );
  TypedQuery<DataObject> query = em.createQuery(q);
  query.setParameter(p1, 100.0);
  query.setParameter(p2, 150.0);
  List resultList = query.getResultList();
        System.out.println(resultList);

        em.close();
        emf.close();
    }

    @Entity
    public static class DataObject {
     private double lon;
        DataObject(Double lon) {
            this.lon = lon;
        }
        public String toString (){
         return String.valueOf(this.lon);
        }
    }
}

 

I my eyes it is exactly the same as you did in your manual. But running this gets me following error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method gt(Expression<? extends Number>, Expression<? extends Number>) in the type CriteriaBuilder is not applicable for the arguments (Path<Object>, ParameterExpression<Double>)
The method lt(Expression<? extends Number>, Expression<? extends Number>) in the type CriteriaBuilder is not applicable for the arguments (Path<Object>, ParameterExpression<Double>)

at MyTestCase.main(MyTestCase.java:30)

Please help...

#2

This should work:

    ParameterExpression<Double> p1 = cb.parameter(Double.class, "p1");
    ParameterExpression<Double> p2 = cb.parameter(Double.class, "p2");
    Path<Double> lon = d.get("lon");
    q.where(
        cb.and(
            cb.gt(lon, p1),
            cb.lt(lon, p1)
        )
    );

A separate variable for the path is required to get a matching type.

But your example demonstrates an unrelated issue with ObjectDB - there is a problem with parameters with no name. Adding names to the parameters (as shown above) fixes it.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

Actually parameters with no name do work. The problem was only with p2 that was defined and set but was not integrated into the query (next build will avoid a NPE in this case).

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#4

Thanks, works fine...


Post Reply

To post a reply and/or subscribe to update notifications - please login