Interface ResultSetMapping<T>

Type Parameters:
T - The result type of the mapping
All Known Implementing Classes:
ColumnMapping, CompoundMapping, ConstructorMapping, EntityMapping, TupleMapping

public sealed interface ResultSetMapping<T> permits CompoundMapping, TupleMapping, EntityMapping<T>, ConstructorMapping<T>, ColumnMapping<T>
Specifies a mapping of the columns of a result set of a SQL query or stored procedure to entities, scalar values, and Java class constructors.

A ResultSetMapping may be instantiated programmatically, for example:

 import static jakarta.persistence.sql.ResultSetMapping.*;

 ...

 var entityMapping =
         entity(Author.class,
                 field(Author_.ssn, "auth_ssn"),
                 embedded(Author_.name,
                         field(Name_.first, "auth_first_name"),
                         field(Name_.last, "auth_last_name")));

 var constructorMapping =
         constructor(Summary.class,
                 column("isbn", String.class),
                 column("title", String.class),
                 column("author", String.class));

 var compoundMapping =
         compound(
                 entity(Author.class),
                 entity(Book.class, field(Book_.isbn, "isbn")),
                 column("sales", BigDecimal.class),
                 constructor(Summary.class, column("isbn"), column("title"))
         );

Alternatively, an instance representing a result set mapping defined using annotations may be obtained via EntityManagerFactory.getResultSetMappings(Class).

A ResultSetMapping may be used to obtain and execute a TypedQuery.

Since:
4.0
See Also:
  • Method Details

    • type

      @Nonnull Class<T> type()
      The result type of the mapping.
    • column

      @Nonnull static <T> ColumnMapping<T> column(@Nonnull String columnName, @Nonnull Class<T> type)
      Construct a mapping for a single column to a scalar value.
      Type Parameters:
      T - The type of the scalar value
      Parameters:
      columnName - The colum name
      type - The Java type of the scalar value
      See Also:
    • column

      @Nonnull static ColumnMapping<Object> column(@Nonnull String columnName)
      Construct a mapping for a single column to a scalar value.
      Parameters:
      columnName - The colum name
      See Also:
    • constructor

      @Nonnull static <T> ConstructorMapping<T> constructor(@Nonnull Class<T> targetClass, @Nonnull MappingElement<?>... arguments)
      Construct a mapping to a constructor of a Java class.
      Type Parameters:
      T - The type of the Java class
      Parameters:
      targetClass - The Java class which declares the constructor
      arguments - Mappings for the constructor parameters, in order
      See Also:
    • compound

      @Nonnull static CompoundMapping compound(@Nonnull MappingElement<?>... elements)
      Construct a mapping which packages a tuple of values as a Java array.
      Parameters:
      elements - Mappings for elements of the type
    • tuple

      @Nonnull static TupleMapping tuple(@Nonnull MappingElement<?>... elements)
      Construct a mapping which packages a tuple of values as an instance of Tuple.
      Parameters:
      elements - Mappings for elements of the type
    • entity

      @Nonnull @SafeVarargs static <T> EntityMapping<T> entity(@Nonnull Class<T> entityClass, @Nonnull MemberMapping<T>... fields)
      Construct a mapping for an entity class.
      Type Parameters:
      T - The entity type
      Parameters:
      entityClass - The Java class of the entity
      fields - Mappings for fields or properties of the entity
      See Also:
    • entity

      @Nonnull @SafeVarargs static <T> EntityMapping<T> entity(@Nonnull Class<T> entityClass, @Nonnull String discriminatorColumn, @Nonnull MemberMapping<? extends T>... fields)
      Construct a mapping for an entity class.
      Type Parameters:
      T - The entity type
      Parameters:
      entityClass - The Java class of the entity
      discriminatorColumn - The name of the column holding the discriminator; an empty string indicates that there is no discriminator column
      fields - Mappings for fields or properties of the entity and of its entity subclasses
      See Also:
    • embedded

      @Nonnull @SafeVarargs static <C,T> EmbeddedMapping<C,T> embedded(@Nonnull Class<? super C> container, @Nonnull Class<T> embeddableClass, @Nonnull String name, MemberMapping<T>... fields)
      Construct a mapping for an embedded object.
      Type Parameters:
      C - The container type
      T - The embeddable type
      Parameters:
      container - The Java class which declares the field holding the embedded object
      embeddableClass - The Java class of the embedded object
      name - The name of the field holding the embedded object
      fields - Mappings for fields or properties of the entity
    • embedded

      @Nonnull @SafeVarargs static <C,T> EmbeddedMapping<C,T> embedded(@Nonnull SingularAttribute<? super C, T> embedded, @Nonnull MemberMapping<T>... fields)
      Construct a mapping for an embedded object.
      Type Parameters:
      C - The container type
      T - The embeddable type
      Parameters:
      embedded - The metamodel attribute representing the field or property holding the embedded object
      fields - Mappings for fields or properties of the entity
    • field

      @Nonnull static <C,T> FieldMapping<C,T> field(@Nonnull Class<? super C> container, @Nonnull Class<T> type, @Nonnull String name, @Nonnull String columnName)
      Construct a mapping for a field or property of an entity or embeddable type.
      Type Parameters:
      C - The type of the entity or embeddable type
      T - The type of the field or property
      Parameters:
      container - The Java class which declares the field or property
      type - The type of the field or property
      name - The name of the field or property
      columnName - The name of the mapped column
      See Also:
    • field

      static <C,T> FieldMapping<C,T> field(@Nonnull SingularAttribute<? super C, T> attribute, @Nonnull String columnName)
      Construct a mapping for a field or property of an entity or embeddable type.
      Type Parameters:
      C - The type of the entity or embeddable type
      T - The type of the field or property
      Parameters:
      attribute - The metamodel attribute representing the field or property
      columnName - The name of the mapped column
      See Also: