|
854 | 854 | *
|
855 | 855 | * <h3>Single entity attribute result type</h3>
|
856 | 856 | *
|
857 |
| - * <p>The {@link Select} annotation chooses the entity attribute. |
| 857 | + * <p>For {@link Find} methods, |
| 858 | + * the {@link Select} annotation chooses the entity attribute. |
858 | 859 | * The result type within the repository method return type must be consistent
|
859 |
| - * with the entity attribute type. For example, if a {@code Person} entity |
| 860 | + * with the entity attribute type. For example, if a {@code Weather} entity |
860 | 861 | * has attributes including {@code year}, {@code month}, {@code day}, and
|
861 | 862 | * {@code precipitation}, of which the latter is of type {@code float},</p>
|
862 | 863 | *
|
|
868 | 869 | * @By("year") int year);
|
869 | 870 | * </pre>
|
870 | 871 | *
|
| 872 | + * <p>For {@link Query} methods, the {@code SELECT} clause specifies a single |
| 873 | + * entity attribute. For example,</p> |
| 874 | + * |
| 875 | + * <pre> |
| 876 | + * @Query("SELECT precipitation FROM Weather " + |
| 877 | + * " WHERE month=?1 AND year=?2" + |
| 878 | + * " ORDER BY precipitation ASC") |
| 879 | + * List<Float> precipitationIn(Month monthOfYear, int year); |
| 880 | + * </pre> |
| 881 | + * |
871 | 882 | * <h3>Multiple entity attributes result type</h3>
|
872 | 883 | *
|
873 |
| - * <p>The repository method return type includes a Java record to represent a |
| 884 | + * <p>For {@link Find} methods, a Java record return type represents a |
874 | 885 | * subset of entity attributes. If the record component names do not match the
|
875 | 886 | * entity attribute names, use the {@link Select} annotation to indicate the
|
876 | 887 | * entity attribute name. For example, if a {@code Person} entity has attributes
|
|
888 | 899 | * <p>The entity class value that is supplied to the {@link Find} annotation can
|
889 | 900 | * be omitted if it is the same as the primary entity type of the repository.</p>
|
890 | 901 | *
|
| 902 | + * <p>For {@link Query} methods, the {@code SELECT} clause lists the entity |
| 903 | + * attributes and the method returns a Java record, which must have a constructor |
| 904 | + * accepting the entity attributes in the order listed within the {@code SELECT} |
| 905 | + * clause. For example,</p> |
| 906 | + * |
| 907 | + * <pre> |
| 908 | + * public record Name(String firstName, |
| 909 | + * String middleName, |
| 910 | + * String surname) {} |
| 911 | + * |
| 912 | + * @Query("SELECT firstName, middleName, lastName FROM Person WHERE ssn=?1") |
| 913 | + * Optional<Name> getName(long socialSecurityNum); |
| 914 | + * </pre> |
| 915 | + * |
| 916 | + * <p>If all record components have names that match the entity attributes or |
| 917 | + * map to a valid entity attribute name via the {@link Select} annotation, |
| 918 | + * then the {@code SELECT} clause can be omitted. For example,</p> |
| 919 | + * |
| 920 | + * <pre> |
| 921 | + * public record Name(String firstName, |
| 922 | + * String middleName, |
| 923 | + * String @Select("lastName") String surname) {} |
| 924 | + * |
| 925 | + * @Query("FROM Person WHERE ssn=?1") |
| 926 | + * Optional<Name> getName(long socialSecurityNum); |
| 927 | + * </pre> |
| 928 | + * |
891 | 929 | * <h2>Repository default methods</h2>
|
892 | 930 | *
|
893 | 931 | * <p>A repository interface may declare any number of {@code default} methods
|
|
0 commit comments