Your First Cup: An Introduction to the Java EE Platform

ProcedureAdd a Named Query to the FirstcupUser Entity

Add a JPQL named query to the FirstcupUser entity that returns the mean average age difference of all firstcup users.

This query uses the AVG aggregate function to return the mean average of all the values of the ageDifference property of the FirstcupUser entities.

  1. Directly before the class definition, paste in the following code:

    @NamedQuery(name="findAverageAgeDifferenceOfAllFirstcupUsers",
    query="SELECT AVG(u.ageDifference) FROM FirstcupUser u)

    The @NamedQuery annotation appears just before the class definition of the entity, and has two required attributes: name, with the unique name for this query; and query, the JPQL query definition.