Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top