JPA Criteria Tutorial [closed]

女生的网名这么多〃 提交于 2019-11-27 10:39:00

The Dynamic, typesafe queries in JPA 2.0 article is a very good one on this topic, actually the best one I've found so far online, even better than the Chapter 23 Using the Criteria API to Create Queries from the Java EE 6 tutorials (that contains some mistakes).

Examples of common queries are here

All examples are in this form:

CriteriaBuilder cb = em.getCriteriaBuilder();

// Query for a List of objects.
CriteriaQuery cq = cb.createQuery();
Root e = cq.from(Employee.class);
cq.where(cb.greaterThan(e.get("salary"), 100000));
Query query = em.createQuery(cq);
List<Employee> result = query.getResultList();

If you are also considering other technologies you should seriously consider querydsl. Main advantages over criteria include shorter code, good readability and similar syntax to regular sql.

Example QueryDSL code here:

JPAQuery query = new JPAQuery(entityManager);
List<Person> persons = query.from(person)
  .where(
    person.firstName.eq("John")),        
  .list(person);

Pro JPA 2: Mastering the Java Persistence API http://books.google.com/books?id=j84hdeHH2PYC

This is the source I find the most useful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!