Top Description Methods
jakarta.persistence

public @Interface ConstructorResult

extends Annotation
Annotations
@Target
@Retention:RUNTIME
Imports
java.lang.annotation.Target, .Retention

Used in conjunction with the SqlResultSetMapping or NamedNativeQuery annotation to map the SELECT clause of a SQL query to a constructor.

Applies a constructor for the target class, passing in as arguments values from the specified columns. All columns corresponding to arguments of the intended constructor must be specified using the columns element of the ConstructorResult annotation in the same order as that of the argument list of the constructor. Any entities returned as constructor results will be in either the new or detached state, depending on whether a primary key is retrieved for the constructed object.

Example:

Query q = em.createNativeQuery( "SELECT c.id, c.name, " + "COUNT(o) as orderCount, " + "AVG(o.price) AS avgOrder " + "FROM Customer c, Orders o " + "WHERE o.cid = c.id " + "GROUP BY c.id, c.name", "CustomerDetailsResult"); @SqlResultSetMapping( name = "CustomerDetailsResult", classes = { @ConstructorResult( targetClass = com.acme.CustomerDetails.class, columns = { @ColumnResult(name = "id"), @ColumnResult(name = "name"), @ColumnResult(name = "orderCount"), @ColumnResult(name = "avgOrder", type = Double.class) }) })
Query q = em.createNativeQuery(
    "SELECT c.id, c.name, " +
        "COUNT(o) as orderCount, " +
        "AVG(o.price) AS avgOrder " +
      "FROM Customer c, Orders o " +
      "WHERE o.cid = c.id " +
      "GROUP BY c.id, c.name",
    "CustomerDetailsResult");

@SqlResultSetMapping(
    name = "CustomerDetailsResult",
    classes = {
        @ConstructorResult(
            targetClass = com.acme.CustomerDetails.class,
            columns = {
                @ColumnResult(name = "id"),
                @ColumnResult(name = "name"),
                @ColumnResult(name = "orderCount"),
                @ColumnResult(name = "avgOrder", type = Double.class)
            })
    })
Since
2.1
See Also
SqlResultSetMapping, NamedNativeQuery, ColumnResult

Method Summary

Modifier and TypeMethod and Description
public ColumnResult[]
columns()

(Required) The mapping of columns in the SELECT list to the arguments of the intended constructor, in order.

public Class<?>
targetClass()

(Required) The class whose constructor is to be invoked.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

columnsback to summary
public ColumnResult[] columns()

(Required) The mapping of columns in the SELECT list to the arguments of the intended constructor, in order.

targetClassback to summary
public Class<?> targetClass()

(Required) The class whose constructor is to be invoked.