0

I'm using Hibernate 3.5.6 and JPA 2.

Here is my code:

  public List<Route> searchRoutesEndingAt(GeoLocation destinationLocation,
        int destinationRangeInMeters, RouteUserPref routeUserPref) {
    Query query = entityManager.createNamedQuery("searchRoutesEndingAt");
    query.setParameter("lat1", destinationLocation.getLatitude());
    query.setParameter("lng1", destinationLocation.getLongitude());
    query.setParameter("destinationRangeInMeters", destinationRangeInMeters);
    try {
        return query.getResultList();
    } catch (NoResultException ex) {
        return null;
    }
}

In the above code I want to filter the result set according to routeUserPref which has various attributes.

sumitarora
  • 580
  • 4
  • 18

2 Answers2

0

You can try the follwing code with Criteria API

Criteria criteria = session.createCriteria(RouteUserPref.class).add(routeUserPref);
return criteria.list();
Nayan Wadekar
  • 11,444
  • 4
  • 50
  • 73
0

You will not be able to do it with EntityManager afaik because it is not included in the standard.

However I think all vendors implements it and for you it would be something like:

Hibernate

And with Hibernate's Criteria API:

// get the native hibernate session
Session session = (Session) getEntityManager().getDelegate();
// create an example from our customer, exclude all zero valued numeric properties 
Example customerExample = Example.create(customer).excludeZeroes();
// create criteria based on the customer example
Criteria criteria = session.createCriteria(Customer.class).add(customerExample);
// perform the query
criteria.list();

Taken from:

JPA - FindByExample

Btw his example from openjpa is incomplete. Ping me in a comment if you want a better version of it.

Community
  • 1
  • 1
Karl Kildén
  • 2,415
  • 22
  • 34