Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/src/main/java/jakarta/data/repository/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@
*
* <p>If the Jakarta Data provider is backed by a Jakarta Persistence provider,
* an automatic query method annotated {@code Delete} can also be annotated
* {@link jakarta.persistence.query.QueryOptions} to supply additional
* query options that are specific to Jakarta Persistence.</p>
* {@code jakarta.persistence.query.StatementOptions} to supply additional
* options that are specific to Jakarta Persistence.
*
* <p>Annotations such as {@code @Find}, {@code @Query}, {@code @Insert},
* {@code @Update}, {@code @Delete}, and {@code @Save} are mutually-exclusive.
Expand All @@ -163,6 +163,7 @@
*
* @see By
*/
// TODO after Persistence milestone, switch @code to @link
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
Expand Down
4 changes: 3 additions & 1 deletion api/src/main/java/jakarta/data/repository/Param.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
* runtime.</p>
*
* <p>Named parameters are not supported on repository methods annotated
* {@code jakarta.persistence.query.NativeQuery}.</p>
* {@link jakarta.persistence.query.NativeQuery} or
* {@code jakarta.persistence.query.NativeStatement}.
*
* @see Query
*/
// TODO after Persistence milestone, switch @code to @link above
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface Param {
Expand Down
4 changes: 3 additions & 1 deletion api/src/main/java/jakarta/data/repository/Select.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
*
* <p>This annotation must not be used in other locations; in particular,
* it must not be used on repository methods annotated
* {@code jakarta.persistence.query.NativeQuery}.</p>
* {@link jakarta.persistence.query.NativeQuery} or
* {@code jakarta.persistence.query.NativeStatement}.
*
* <p>Sort criteria for a repository method that returns a projection
* can include any of the returned entity attributes that are of sortable type.
Expand All @@ -60,6 +61,7 @@
* @see Find
* @since 1.1
*/
// TODO after Persistence milestone, switch @code to @link
@Documented
@Repeatable(Select.List.class)
@Retention(RetentionPolicy.RUNTIME)
Expand Down
39 changes: 24 additions & 15 deletions api/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.query.JakartaQuery;
// TODO after Persistence milestone, add these and switch @code to @link:
// import jakarta.persistence.query.JakartaStatement;
import jakarta.persistence.query.NativeQuery;
// import jakarta.persistence.query.NativeStatement;
import jakarta.transaction.Status;
import jakarta.transaction.Transactional;
import jakarta.validation.ConstraintViolationException;
Expand Down Expand Up @@ -213,8 +216,9 @@
* <tr style="vertical-align: top">
* <td><a href="#QueryLanguageMethod">Query Language Method</a></td>
* <td>A repository method is annotated {@link Query @Query},
* {@link JakartaQuery @JakartaQuery}, or {@link NativeQuery @NativeQuery}
* to supply a query string.
* {@link JakartaQuery @JakartaQuery}, {@code @JakartaStatement},
* {@link NativeQuery @NativeQuery}, or {@code @NativeStatement}
* to supply a query or statement.
* {@snippet lang="java":
* @Query("SELECT COUNT(THIS) FROM Product WHERE department = ?1")
* int totalIn(Department department);
Expand Down Expand Up @@ -642,22 +646,26 @@
*
* <h2>Jakarta Persistence query methods</h2>
*
* <p>The Jakarta Persistence specification offers two additional query
* annotations that can annotate repository methods in place of {@link Query}:
* <p>The Jakarta Persistence specification offers additional annotations
* that can annotate repository methods in place of {@link Query} to
* distinguish between queries and statements and allow for additional
* query languages:
*
* <ul>
* <li>{@link NativeQuery} - supplies a native
* SQL query instead of a query that is written in Jakarta Persistence Query
* Language (JPQL) or the Jakarta Common Query Language (JCQL).</li>
* <li>{@link JakartaQuery} - behaves identically to
* Jakarta Data's {@code Query} annotation in allowing JPQL and JCQL to be
* supplied.</li>
* <li>{@link NativeQuery} - supplies a native SQL query that returns a
* result set or aggregate result.</li>
* <li>{@code NativeStatement} - supplies a native SQL statement that
* returns a row count.</li>
* <li>{@link JakartaQuery} - supplies a Jakarta Persistence Query Language
* (JPQL) or Jakarta Common Query Language (JCQL) {@code SELECT} query.</li>
* <li>{@code JakartaStatement} - supplies a JPQL or JCQL {@code DELETE} or
* {@code UPDATE} statement.</li>
* </ul>
*
* <p>A Jakarta Data provider is not required to support JPQL or native SQL
* for repository query methods that require generating or modifying the query,
* including repository methods with any of the following:
* </p>
* for repository methods that require generating or modifying the given
* query or statement value, including repository methods with any of the
* following:
*
* <ul>
* <li>{@linkplain Select Projections}</li>
Expand All @@ -669,8 +677,9 @@
* <li>{@linkplain PageRequest#requestTotal() Total pages or results}</li>
* </ul>
*
* <p>{@linkplain Param Named parameters} can be used for JPQL queries, but not
* for native SQL queries. Use positional parameters instead. For example,
* <p>{@linkplain Param Named parameters} can be used for JPQL queries and
* statements, but not for native SQL queries and statements. Use positional
* parameters instead. For example,
*
* {@snippet lang="java":
* @NativeQuery("SELECT * FROM Person WHERE ssn = ? AND alive = ?")
Expand Down
18 changes: 12 additions & 6 deletions spec/src/main/asciidoc/repository.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,29 @@ The Jakarta Data implementation automatically recognizes the query annotations i

The Jakarta Persistence specification defines several annotations in the `jakarta.persistence.query` package that can be used on Jakarta Data repositories.

- `@NativeQuery`, which can annotate a Jakarta Data repository method in place of Jakarta Data's `@Query` to supply a native SQL query.
- `@NativeQuery`, which can annotate a Jakarta Data repository method in place of Jakarta Data's `@Query` to supply a native SQL query that returns a result set or aggregate result.

- `@JakartaQuery`, which can annotate a Jakarta Data repository method in place of Jakarta Data's `@Query` to supply a JPQL query.
- `@NativeStatement`, which can annotate a Jakarta Data repository method in place of Jakarta Data's `@Query` to supply a native SQL statement that returns a row count.

- `@JakartaQuery`, which can annotate a Jakarta Data repository method in place of Jakarta Data's `@Query` to supply a JPQL `SELECT` query.

- `@JakartaStatement`, which can annotate a Jakarta Data repository method in place of Jakarta Data's `@Query` to supply a JPQL `DELETE` or `UPDATE` statement.

- `@QueryOptions`, which can annotate a Jakarta Data repository method to supply additional options defined by Jakarta Persistence. The Jakarta Data repository method must also

** be annotated `@Query`, `@JakartaQuery`, or `@NativeQuery` and perform a `SELECT` operation,
** be annotated `@Query`, `@JakartaQuery`, or `@NativeQuery` and perform a `SELECT` operation, or

** be annotated `@Find`.

** be annotated `@Find`,
- `@StatementOptions`, which can annotate a Jakarta Data repository method to supply additional options defined by Jakarta Persistence. The Jakarta Data repository method must also

** be annotated `@Query`, `@JakartaQuery`, or `@NativeQuery` and perform a `DELETE` or `UPDATE` operation, or
** be annotated `@Query`, `@JakartaStatement`, or `@NativeStatement` and perform a `DELETE` or `UPDATE` operation, or

** be annotated `@Delete` and not define a lifecycle operation.

==== Limitations of JPQL and native SQL queries

The Jakarta Data provider is not required to support JPQL or native SQL for repository query methods that require generating or modifying the query, including repository methods with any of the following:
The Jakarta Data provider is not required to support JPQL or native SQL for repository methods that require generating or modifying the query or statement value, including repository methods with any of the following:

- Projections
- `Restriction` and `Constraint`-typed parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ SELECT AVG(p.price)
@QueryOptions(flush = QueryFlushMode.FLUSH)
double averagePrice(String productNumber);

// TODO after Persistence milestone, switch to StatementOptions
@Delete
@QueryOptions(flush = QueryFlushMode.FLUSH)
long deleteByPrice(@By("price") double price);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ public static WebArchive createDeployment() {
@Inject
UserTransaction tran;

@Assertion(id = "1312", strategy = """
Within an active transaction, modify a persisted entity without
explicitly flushing, then verify that a repository method annotated
@Delete and @StatementOptions(flush = QueryFlushMode.FLUSH) deletes
the entity based on the modified state.
""")
public void testDeleteWithStatementOptions() throws Exception {
inventory.erase();

Product product = Product.of("tennis racket",
82.99,
"TEST-PROD-1020",
Department.SPORTING_GOODS);

inventory.persist(product);

tran.begin();
product = inventory.merge(product);
product.setPrice(84.98);

assertEquals(1L,
inventory.deleteByPrice(84.98));

tran.commit();
}

@Assertion(id = "471", strategy = """
Use the Detach annotation to detach entities from the persistence context.
Verify that modifications to detached entities are not automatically
Expand Down Expand Up @@ -299,32 +325,6 @@ public void testFindWithQueryOptions() throws Exception {
inventory.remove(found);
}

@Assertion(id = "1312", strategy = """
Within an active transaction, modify a persisted entity without
explicitly flushing, then verify that a repository method annotated
@Delete and @QueryOptions(flush = QueryFlushMode.FLUSH) deletes the
entity based on the modified state.
""")
public void testDeleteWithQueryOptions() throws Exception {
inventory.erase();

Product product = Product.of("tennis racket",
82.99,
"TEST-PROD-1020",
Department.SPORTING_GOODS);

inventory.persist(product);

tran.begin();
product = inventory.merge(product);
product.setPrice(84.98);

assertEquals(1L,
inventory.deleteByPrice(84.98));

tran.commit();
}

@Assertion(id = "1312", strategy = """
Within an active transaction, modify a persisted entity without
explicitly flushing, then verify that a repository method annotated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ SELECT AVG(p.price)
""")
double averagePrice(Department department);

// TODO must switch to @NativeStatement after a milestone release of the
// Jakarta Persistence API with NativeStatement is available
@NativeQuery("""
DELETE FROM Product
WHERE UPPER(name) = UPPER(?) AND versionNum = ?
""")
int deleteIfNamed(String productName, long version);

// TODO must switch to @JakartaStatement after a milestone release of the
// Jakarta Persistence API with JakartaStatement is available
@JakartaQuery("""
DELETE FROM Product p
WHERE p.productNum LIKE ?1
Expand Down Expand Up @@ -125,6 +129,8 @@ List<Product> matchesProductNumPattern(
@Delete
void removeMultiple(Product... products);

// TODO must switch to @NativeStatement after a milestone release of the
// Jakarta Persistence API in which NativeStatement is available
@NativeQuery("""
UPDATE Product
SET price = ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static WebArchive createDeployment() {
Catalog catalog;

@Assertion(id = "1218", strategy = """
Use a repository method annotated JakartaQuery to run a JPQL
Use a repository method annotated JakartaStatement to run a JPQL
DELETE operation.
""")
public void testJPQLDelete() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void testSQLSelectEntityAttribute() {
}

@Assertion(id = "1218", strategy = """
Use a repository method annotated NativeQuery to run a SQL
Use a repository method annotated NativeStatement to run a SQL
UPDATE statement.
""")
public void testSQLUpdate() {
Expand Down