Named Queries
  • An entity may declare named queries, using the @NamedQuery annotation
@NamedQuery Annotation Example  
  @Entity
  @NamedQuery(name="employeeBySsn" query="select e from Employee e where e.ssn = :ssn")
  public class Employee {
    ...
  }

  public class EmployeeDao {
    ...
    public Employee getEmployeeBySsn( String ssn ) {
      Query query = entityManager.getNamedQuery( "employeeBySsn" );
      query.setParameter( "ssn", ssn );
      return ( Employee )query.getSingleResult();
    }
    ...
  }
  • Multiple named queries can be defined by using the @NamedQueries annotation
@NamedQueries Annotation Example