How to use named queries with JPA

You can define several named queries on an entity by using the @NamedQueries and @NamedQuery annotations. For each named query you need to define a name and the jpql query itself:

import javax.persistence.*;

@Entity
@Access(AccessType.FIELD)
@Table(name = PartnerInitialLoad.TABLE_NAME)
@NamedQueries({
  @NamedQuery(
      name = FIND_MAX_PARTNERNUMMER,
      query = "select max(p.partnernummer) from PartnerInitialLoad p"),
  @NamedQuery(
      name = FIND_PARTNER_BY_STATUS,
      query =
          "select p from PartnerInitialLoad p where p.status = :status order by p.partnernummer asc")
})
public class PartnerInitialLoad {
  public static final String TABLE_NAME = "T_PARTNER_INITIAL_LOAD";

  public static final String FIND_MAX_PARTNERNUMMER = "findMaxPartnernummer";
  public static final String FIND_PARTNER_BY_STATUS = "findPartnerByStatus";

  public static final String PARAM_STATUS = "status"; //the value here has to match the one in jpql, here "status"

 // further entity details emitted for brevity
}

Then use the named queries in your repository services. For that use the createNamedQuery method of the EntityManager, which expects the name of the query you defined in the metadata, plus the type of the query result:

@Stateless
public class PartnerInitialLoadRepository {

  @Inject private EntityManager em;

  public List<PartnerInitialLoad> getPartnersByStatus(Integer chunkSize, String status) {
    var query =
        em.createNamedQuery(PartnerInitialLoad.FIND_UNPROCESSED_PARTNER, PartnerInitialLoad.class);
    query.setParameter(PartnerInitialLoad.PARAM_STATUS, status);
    query.setMaxResults(chunkSize);

    return query.getResultList();
  }

  public int getMaxPartnernummer() {
    var query = em.createNamedQuery(PartnerInitialLoad.FIND_MAX_PARTNERNUMMER, Integer.class);
    var singleResult = query.getSingleResult();

    return singleResult == null ? 0 : singleResult;
  }
}

Shared with from Codever. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Codever is open source on Github ⭐🙏

Subscribe to our newsletter for more code resources and news

Adrian Matei (aka adixchen)

Adrian Matei (aka adixchen)
Life force expressing itself as a coding capable human being

routerLink with query params in Angular html template

routerLink with query params in Angular html template code snippet Continue reading