Skip to content

Commit 040f0c6

Browse files
authored
Merge pull request #1009 from njr-11/539-fix-typo-and-include-example-of-Query-with-SELECT-record
Fix typo, include example Query with SELECT returning record, and idea for more concise usage
2 parents 94e4e05 + e8d9bf9 commit 040f0c6

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

api/src/main/java/jakarta/data/repository/Query.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@
3434
*
3535
* <p>The required {@link #value} member specifies the JDQL or JPQL query as a string.</p>
3636
*
37-
* <p>For {@code select} statements, the return type of the query method must be consistent with the type returned by
38-
* the query. For queries with an explicit {@code select} clause:</p>
37+
* <p>For {@code select} statements, the return type of the query method must be
38+
* consistent with the type returned by the query. An explicit {@code SELECT} clause
39+
* can be omitted when the query returns the entity or a Java record for which the
40+
* record component names all map to entity attribute names, either by having the
41+
* same name as the entity attribute or via the record component being annotated with
42+
* the {@link Select} annotation. For queries with an explicit {@code select} clause:</p>
3943
* <ul>
4044
* <li>if the {@code select} list contains more than one item, the query return type must be a Java record type, and the
4145
* elements of the tuple are repackaged as an instance of the query return type by calling a constructor of the

api/src/main/java/module-info.java

+41-3
Original file line numberDiff line numberDiff line change
@@ -854,9 +854,10 @@
854854
*
855855
* <h3>Single entity attribute result type</h3>
856856
*
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.
858859
* 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
860861
* has attributes including {@code year}, {@code month}, {@code day}, and
861862
* {@code precipitation}, of which the latter is of type {@code float},</p>
862863
*
@@ -868,9 +869,19 @@
868869
* &#64;By("year") int year);
869870
* </pre>
870871
*
872+
* <p>For {@link Query} methods, the {@code SELECT} clause specifies a single
873+
* entity attribute. For example,</p>
874+
*
875+
* <pre>
876+
* &#64;Query("SELECT precipitation FROM Weather " +
877+
* " WHERE month=?1 AND year=?2" +
878+
* " ORDER BY precipitation ASC")
879+
* List&lt;Float&gt; precipitationIn(Month monthOfYear, int year);
880+
* </pre>
881+
*
871882
* <h3>Multiple entity attributes result type</h3>
872883
*
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
874885
* subset of entity attributes. If the record component names do not match the
875886
* entity attribute names, use the {@link Select} annotation to indicate the
876887
* entity attribute name. For example, if a {@code Person} entity has attributes
@@ -888,6 +899,33 @@
888899
* <p>The entity class value that is supplied to the {@link Find} annotation can
889900
* be omitted if it is the same as the primary entity type of the repository.</p>
890901
*
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+
* &#64;Query("SELECT firstName, middleName, lastName FROM Person WHERE ssn=?1")
913+
* Optional&lt;Name&gt; 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 &#64;Select("lastName") String surname) {}
924+
*
925+
* &#64;Query("FROM Person WHERE ssn=?1")
926+
* Optional&lt;Name&gt; getName(long socialSecurityNum);
927+
* </pre>
928+
*
891929
* <h2>Repository default methods</h2>
892930
*
893931
* <p>A repository interface may declare any number of {@code default} methods

0 commit comments

Comments
 (0)