EntityManager entityManager = Persistence.createEntityManagerFactory(
"JPA").createEntityManager();
String jpql = "select p from Person p where p.age > 20";
Query qry = entityManager.createQuery(jpql);
@SuppressWarnings("unchecked")
List<Person> jpqlResult = qry.getResultList();
if (jpqlResult != null) {
for (Person personList : jpqlResult)
System.out.println(" -Using JPQL Criteria API Age: "
+ personList.getAge());
}
// using JPA2.0 Criteria API
CriteriaBuilder qb = entityManager.getCriteriaBuilder();
CriteriaQuery<Person> c = qb.createQuery(Person.class);
Root<Person> pp = c.from(Person.class);
Predicate condition = qb.gt(pp.get(Person_.age), 20);
c.where(condition);
TypedQuery<Person> q = entityManager.createQuery(c);
List<Person> jpaCriteriaResult = q.getResultList();
if (jpaCriteriaResult != null) {
for (Person personList : jpaCriteriaResult)
System.out.println(" -Using JPA2.0 Criteria API Age: "
+ personList.getAge());
}
No comments:
Post a Comment