Querying - rafalkieras/jpa-kss GitHub Wiki
##JPQL
JPQL stands for Java Persistence Query Language and it is a platform-independent query language. It operates on the entity metamodel, not the database schema. It is very similiar to the SQL language.
Created by the createQuery method of the EntityManager:
entityManager.createQuery("from Employee").getResultList()
Defined as a named queries using either annotations or xml.
@NamedQuery(name = "getNames", query = "select e.firstName from Employee e")
<named-query name="getNames">
<query>select e.firstName from Employee e</query>
</named-query>
##Criteria query
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Employee> q = cb.createQuery(Employee.class);
Root<Country> e = q.from(Employee.class);
q.select(e);
TypedQuery<Employee> typedQuery = em.createQuery(q);
List<Employee> list = typedQuery.getResultList();
-
javax.persistence.query.timeout
- sets maximum query execution time in milliseconds. A QueryTimeoutException is thrown if timeout is exceeded. -
javax.persistence.lock.timeout
- sets maximum waiting time for pessimistic locks, when pessimistic locking of query results is enabled. A LockTimeoutException is thrown if timeout is exceeded.
This hints can be set on EntityManager level:
em.setProperty("javax.persistence.query.timeout", 3000)
Or on the query itself:
query.setHint("javax.persistence.query.timeout", 5000)
Or for the whole persistence unit in persistence.xml:
<properties>
<property name="javax.persistence.query.timeout" value="1000"/>
</properties>