3

Is it possible to use CriteriaQuery with JPA 1.0. I guess JPA 2.0 not available with Java Se ( version -- Java(TM) SE Runtime Environment (build 1.6.0_16-b01)) . I tied to use,

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Test> cq = cb.createQuery(Test.class);
Root<Test> test= cq.from(Test.class);

....

But could not find the definition of CriteriaBuilder in javax.persistence.*; (tried import javax.persistence.criteria.CriteriaBuilder; as well)

If it is not possible, what is the best option I have. I use hibernate in the backend, but still is there a non proprietary way to do this? if not how to do in hibernate?

thanks.

2
  • Is there any specific reason you can't upgrade to JPA2?
    – Ophidian
    Commented Aug 13, 2010 at 15:33
  • the framework I use comes with hibernate (older ver supports JPA 1.0) .. I may try to get it updated..
    – bsr
    Commented Aug 13, 2010 at 16:22

2 Answers 2

7

Is it possible to use CriteriaQuery with JPA 1.0.

No, JPA 1.0 didn't specify a Criteria API, the Criteria API is part of the new stuff from JPA 2.0.

I guess JPA 2.0 not available with Java SE

Neither JPA 1.0 nor JPA 2.0 are part of Java SE (bundled with). You CAN however use JPA (1.0 or 2.0) in Java SE.

(...) But could not find the definition of CriteriaBuilder in javax.persistence.*

As mentioned, this is not part of the JPA 1.0 API, you won't find it if you're not using a JPA 2.0 implementation.

If it is not possible, what is the best option I have. I use hibernate in the backend, but still is there a non proprietary way to do this? if not how to do in hibernate?

The only option in your case would be to use Hibernate's proprietary API for Criteria Queries. You'll need to access the Session to do so:

org.hibernate.Session session = (Session) em.getDelegate(); 
Criteria crit = session.createCriteria(Cat.class);
crit.setMaxResults(50);
List cats = crit.list();

References

0
1

JPA2 is most certainly available for Java SE but it does not ship with the JDK by default.

Since the JPA1 spec doesn't specify things like the getCriteriaBuilder() method on the EntityManager, you're unlikely to be able to use them easily without upgrading to JPA2.

The hibernate-entitymanager.jar file for Hibernate 3.5+ implements the JPA2 api for Hibernate and should allow you to use the new JPA features just fine with Java SE.

For more details, I'd suggest the reference docs for Hibernate Core and Hibernate Entity Manager, specifically the sections in the latter pertaining to obtaining an EntityManager in Java SE.

1
  • thanks for the detailed answer.. will accept the answer if no other comments today.. thanks
    – bsr
    Commented Aug 13, 2010 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.