2.10.2.8.2. JPQL Constructor Expressions

A constructor may be used in the SELECT list to return one or more Java instances. The specified class is not required to be an entity or to be mapped to the database. The constructor name must be fully qualified.

If an entity class name is specified in the SELECT NEW clause, the resulting entity instances are in the new state.

If a single_valued_path_expression or identification_variable that is an argument to the constructor references an entity, the resulting entity instance referenced by that single_valued_path_expression or identification_variable will be in the managed state.

If PublisherInfo is an entity class, the following 2 queries return instances of PublisherInfo that will be in the new state. In the second example, mag is an identification_variable passed as an argument to the constructor PublisherInfo(Magazine mag); the entity instances of Magazine created during query evaluation will be in the managed state. Example:

SELECT NEW com.company.PublisherInfo(pub.id, pub.revenue, mag.price)
    FROM Publisher pub JOIN pub.magazines mag WHERE mag.price  5.00
SELECT NEW com.company.PublisherInfo(mag)
    FROM Publisher pub JOIN pub.magazines mag WHERE mag.price  5.00