This content has been marked as final.
Show 3 replies
-
1. Re: Queries with optional arguments
pmuir Nov 5, 2007 8:23 AM (in response to ericjava)Use
org.hibernate.Session session = (Session) entityManager.getDelegate();
to get the Hibernate Session out and use the Criteria API. Yes, this does make your application non-portable to other JPA implementations, but that is the price you pay. -
2. Re: Queries with optional arguments
matt.drees Nov 5, 2007 12:58 PM (in response to ericjava)You could look into using Krank's JPA Criteria. I hope to at some point.
http://www-128.ibm.com/developerworks/blogs/page/JEE?entry=criteria_qbe_dsl_for_jpa -
3. Re: Queries with optional arguments
damianharvey Nov 6, 2007 5:08 AM (in response to ericjava)I haven't looked into the delegate/criteria stuff but my lazy-man's way of doing this is:
List<Customer> customers = entityManager.createQuery("select c from Customer as c "+ " where c.customerId >= :minCustomerId"+ " and c.customerId <= :maxCustomerId") .setParameter("minCustomerId", selectedCustomer==null?Long.MIN_VALUE:selectedCustomer.customerId) .setParameter("maxCustomerId", selectedCustomer==null?Long.MAX_VALUE:selectedCustomer.customerId) .getResultList();
Cheers,
Damian.