Java Reference
In-Depth Information
public void persist(final T entity) {
em.persist(entity);
}
public List<T> findAll() {
final CriteriaQuery<T> criteriaQuery =
em.getCriteriaBuilder().createQuery(clazz);
criteriaQuery.select(criteriaQuery.from(clazz));
return
em.createQuery(criteriaQuery).getResultList();
}
public void deleteAll() {
final CriteriaDelete<T> criteriaDelete =
em.getCriteriaBuilder().createCriteriaDelete(clazz);
criteriaDelete.from(clazz);
em.createQuery(criteriaDelete).executeUpdate();
}
}
As you can see, both SeatDao and SeatTypeDao beans extend the generic Ab-
stractDao class. It wraps EntityManager and provides basic type-safe CRUD op-
erations such as findAll , persist , and so on using the JPA Criteria API. JPA allows
execution of the following three types of queries:
Native SQL : These queries use the standard SQL language. When using this type
of queries, you have to remember queries can be incompatible when migrating
between different databases.
Java Persistence Query Language ( JPQL ): These queries can be formed using
special language similar to SQL. In practice, this method is often hard to maintain
without good IDE support, especially during refactoring. These queries can also
be compiled at startup, which means that they are not resolved multiple times.
Finally, they can be used by a caching mechanism to avoid unnecessary database
operations for queries that are called frequently. You can define a query and its
name in the entity in the @NamedQuery(name="…", query="…") annota-
tion.
Search WWH ::




Custom Search