In most parts of your life such a programmer, you will use JPQL. It's easy to work. However, in some cases, the JPQL cannot be enough.

Sometimes, there are scenarios where is necessary to use a specific query feature of the database that your JPA implementation (such as hibernate) doesn't give support.

The annotation used when you are using JPQL is the @NamedQuerie that you add in your entity. Another option is to add this query inside an XML.

@NamedQueries({ @NamedQuery(name = "nameOfYourQuery",
  query = "SELECT e FROM YourEntity e"),
public class YourEntity {
....
}

To execute this query frequently you use something like this:

entityManager.createNamedQuery("nameOfYourQuery", YourEntity.class);

To use a native query is similar, but the natural return is a list or an array:

Query q = em.createNativeQuery("SELECT e.name FROM YourEntity e");
List<Object[]> entities = q.getResultList();

In case you have a class that represents the result you can call your native query by name:

Query q = em.createNativeQuery("yourNativeQueryName", YourEntity.class);
List yourEntity = q.getResultList();

The declaration of your query can be using the annotation @SqlResultSetMapping. It needs the annotation @ColumnResult that will represent all the columns in the result of your query.

@SqlResultSetMapping(name="nameYourNativeQueryMap",
   columns = { @ColumnResult(name = "id"),
               @ColumnResult(name = "name")})

@NamedNativeQuery( name = "yourNativeQueryName",
    query = "SELECT e.id, e.name FROM YourEntity e ",
    resultSetMapping = "nameYourNativeQueryMap")

References