开发者

CriteriaQuery with JPA 1.0

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开发者_JAVA百科.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.


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

  • Chapter 15. Criteria Queries


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜