4 Haziran 2018 Pazartesi

JPA TypedQuery Arayüzü

Şu satırı dahil ederiz.
import javax.persistence.TypedQuery;
JPA Query arayüzünden kalıtır. Açıklaması şöyle.
TypedQuery gives you an option to mention the type of entity when you create a query and therefore any operation thereafter does not need an explicit cast to the intended type. Whereas the normal Query API does not return the exact type of Object you expect and you need to cast.
Şöyle yaparız.
CriteriaQuery<Customer> select = ...;

TypedQuery<Customer> typedQuery = entityManager.createQuery(select);
getResult metodu
Şöyle yaparız.
String hql = "select count(me.grp) from  MyEntity me group by grp";
TypedQuery<Integer> query = entityManager.createQuery(hql, Integer.class);
List<Integer> result = query.getResult();
getResultList metodu
Şöyle yaparız.
TypedQuery<SimpleEntity> q = 
        em.createQuery("select t from SimpleEntity t", SimpleEntity.class);

List<SimpleEntity> listOfSimpleEntities = q.getResultList();
for (SimpleEntity entity : listOfSimpleEntities) {
    // do something useful with entity;
setFirstResult metodu
Şöyle yaparız.
public List<Customer> getByCompany(Long id, int page, int size) {
  TypedQuery<Customer> typedQuery = ...;

  typedQuery.setFirstResult(page * size);

  return typedQuery.getResultList();
setMaxResults metodu
Şöyle yaparız.
int size = ...;

TypedQuery<Customer> typedQuery = ...;


Hiç yorum yok:

Yorum Gönder