Skip to content

Commit 8a3b40d

Browse files
committed
alterative design for static query annotations
1 parent ec2a261 commit 8a3b40d

File tree

8 files changed

+174
-279
lines changed

8 files changed

+174
-279
lines changed

api/src/main/java/jakarta/persistence/TypedQueryReference.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
/**
2121
* A reference to a named query declared via the {@link NamedQuery}
2222
* or {@link NamedNativeQuery} annotations, or using one of
23-
* {@link jakarta.persistence.query.ReadQuery},
24-
* {@link jakarta.persistence.query.WriteQuery},
25-
* {@link jakarta.persistence.query.NativeReadQuery}, or
26-
* {@link jakarta.persistence.query.NativeWriteQuery}.
23+
* {@link jakarta.persistence.query.StaticQuery}, or
24+
* {@link jakarta.persistence.query.StaticNativeQuery}.
2725
*
2826
* @param <R> an upper bound on the result type of the query
2927
*
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2008, 2023 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
// Contributors:
14+
// Petros Splinakis - 2.2
15+
// Linda DeMichiel - 2.1
16+
// Linda DeMichiel - 2.0
17+
18+
package jakarta.persistence.query;
19+
20+
import jakarta.persistence.*;
21+
22+
import java.lang.annotation.Repeatable;
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.Target;
25+
26+
import static java.lang.annotation.ElementType.METHOD;
27+
import static java.lang.annotation.ElementType.TYPE;
28+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
29+
30+
@Target(METHOD)
31+
@Retention(RUNTIME)
32+
public @interface NativeQueryMapping {
33+
/**
34+
* Specifies the result set mapping to entities.
35+
*/
36+
EntityResult[] entities() default {};
37+
38+
/**
39+
* Specifies the result set mapping to constructors.
40+
*/
41+
ConstructorResult[] classes() default {};
42+
43+
/**
44+
* Specifies the result set mapping to scalar values.
45+
*/
46+
ColumnResult[] columns() default {};
47+
}

api/src/main/java/jakarta/persistence/query/NativeWriteQuery.java

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0 which is available at
6+
* http://www.eclipse.org/legal/epl-2.0,
7+
* or the Eclipse Distribution License v. 1.0 which is available at
8+
* http://www.eclipse.org/org/documents/edl-v10.php.
9+
*
10+
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11+
*/
12+
13+
// Contributors:
14+
// Gavin King - 4.0
15+
16+
package jakarta.persistence.query;
17+
18+
import jakarta.persistence.CacheRetrieveMode;
19+
import jakarta.persistence.CacheStoreMode;
20+
import jakarta.persistence.LockModeType;
21+
import jakarta.persistence.QueryHint;
22+
23+
import java.lang.annotation.Retention;
24+
import java.lang.annotation.Target;
25+
26+
import static java.lang.annotation.ElementType.METHOD;
27+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
28+
29+
@Target(METHOD)
30+
@Retention(RUNTIME)
31+
public @interface ReadQueryOptions {
32+
/**
33+
* The {@linkplain CacheStoreMode cache store mode} to use.
34+
* @see jakarta.persistence.Query#setCacheStoreMode
35+
*/
36+
CacheStoreMode cacheStoreMode() default CacheStoreMode.USE;
37+
38+
/**
39+
* The {@linkplain CacheRetrieveMode cache retrieve mode}
40+
* to use.
41+
* @see jakarta.persistence.Query#setCacheRetrieveMode
42+
*/
43+
CacheRetrieveMode cacheRetrieveMode() default CacheRetrieveMode.USE;
44+
45+
/**
46+
* A query timeout in milliseconds.
47+
* By default, there is no timeout.
48+
* @see jakarta.persistence.Query#setTimeout
49+
*/
50+
int timeout() default -1;
51+
52+
/**
53+
* Query properties and hints.
54+
* May include vendor-specific query hints.
55+
* @see jakarta.persistence.Query#setHint(String, Object)
56+
*/
57+
QueryHint[] hints() default {};
58+
59+
/**
60+
* The lock mode type to use in query execution.
61+
* If a {@code lockMode} other than {@link LockModeType#NONE}
62+
* is specified, the query must be executed in a transaction
63+
* and the persistence context joined to the transaction.
64+
* <p> If a lock mode is explicitly specified for a
65+
* {@linkplain StaticNativeQuery native query}, the behavior
66+
* is undefined and unportable between persistence providers.
67+
* @see jakarta.persistence.Query#setLockMode
68+
*/
69+
LockModeType lockMode() default LockModeType.NONE;
70+
}

api/src/main/java/jakarta/persistence/query/NativeReadQuery.java renamed to api/src/main/java/jakarta/persistence/query/StaticNativeQuery.java

Lines changed: 7 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
package jakarta.persistence.query;
1717

18-
import jakarta.persistence.*;
19-
2018
import java.lang.annotation.Retention;
2119
import java.lang.annotation.Target;
2220

@@ -35,14 +33,15 @@
3533
* Jakarta Data repository. The return type of the method must agree with
3634
* the type returned by the query.
3735
* {@snippet :
38-
* interface Library {
39-
* @NativeReadQuery(query="select * from books where title like ?1")
36+
* import jakarta.persistence.CacheStoreMode;interface Library {
37+
* @StaticNativeQuery("select * from books where title like ?1")
38+
* @ReadQueryOptions(cacheStoreMode = CacheStoreMode.BYPASS)
4039
* List<Book> findBooksByTitle(String title);
4140
*
42-
* @NativeReadQuery(query="select * from books where isbn = ?1")
41+
* @StaticNativeQuery("select * from books where isbn = ?1")
4342
* Book getBookWithIsbn(String isbn);
4443
* }
45-
* }
44+
*}
4645
* <p> A method return type <em>agrees</em> with the type returned by the
4746
* query if either:
4847
* <ul>
@@ -79,67 +78,9 @@
7978
*/
8079
@Target(METHOD)
8180
@Retention(RUNTIME)
82-
public @interface NativeReadQuery {
83-
81+
public @interface StaticNativeQuery {
8482
/**
8583
* The query string in the native SQL dialect of the database.
8684
*/
87-
String query();
88-
89-
/**
90-
* The name of a {@link SqlResultSetMapping}, as defined in metadata.
91-
* The named result set mapping is used to interpret the result set
92-
* of the native SQL query.
93-
*
94-
* <p>Alternatively, the elements {@link #entities}, {@link #classes},
95-
* and {@link #columns} may be used to specify a result set mapping.
96-
* These elements may not be used in conjunction with
97-
* {@code resultSetMapping}.
98-
*/
99-
String resultSetMapping() default "";
100-
101-
/**
102-
* Specifies the result set mapping to entities.
103-
* May not be used in combination with {@link #resultSetMapping}.
104-
*/
105-
EntityResult[] entities() default {};
106-
107-
/**
108-
* Specifies the result set mapping to constructors.
109-
* May not be used in combination with {@link #resultSetMapping}.
110-
*/
111-
ConstructorResult[] classes() default {};
112-
113-
/**
114-
* Specifies the result set mapping to scalar values.
115-
* May not be used in combination with {@link #resultSetMapping}.
116-
*/
117-
ColumnResult[] columns() default {};
118-
119-
/**
120-
* The {@linkplain CacheStoreMode cache store mode} to use.
121-
* @see jakarta.persistence.Query#setCacheStoreMode
122-
*/
123-
CacheStoreMode cacheStoreMode() default CacheStoreMode.USE;
124-
125-
/**
126-
* The {@linkplain CacheRetrieveMode cache retrieve mode}
127-
* to use.
128-
* @see jakarta.persistence.Query#setCacheRetrieveMode
129-
*/
130-
CacheRetrieveMode cacheRetrieveMode() default CacheRetrieveMode.USE;
131-
132-
/**
133-
* A query timeout in milliseconds.
134-
* By default, there is no timeout.
135-
* @see jakarta.persistence.Query#setTimeout
136-
*/
137-
int timeout() default -1;
138-
139-
/**
140-
* Query properties and hints.
141-
* May include vendor-specific query hints.
142-
* @see jakarta.persistence.Query#setHint(String, Object)
143-
*/
144-
QueryHint[] hints() default {};
85+
String value();
14586
}

api/src/main/java/jakarta/persistence/query/ReadQuery.java renamed to api/src/main/java/jakarta/persistence/query/StaticQuery.java

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515

1616
package jakarta.persistence.query;
1717

18-
import jakarta.persistence.CacheRetrieveMode;
19-
import jakarta.persistence.CacheStoreMode;
20-
import jakarta.persistence.LockModeType;
21-
import jakarta.persistence.QueryHint;
22-
2318
import java.lang.annotation.Retention;
2419
import java.lang.annotation.Target;
2520

@@ -39,13 +34,14 @@
3934
* the type returned by the query.
4035
* {@snippet :
4136
* interface Library {
42-
* @ReadQuery(query="from Book where title like :title")
37+
* @StaticQuery("from Book where title like :title")
38+
* @ReadQueryOptions(cacheStoreMode = CacheStoreMode.BYPASS)
4339
* List<Book> findBooksByTitle(String title);
4440
*
45-
* @ReadQuery(query="from Book where isbn = :isbn")
41+
* @StaticQuery("from Book where isbn = :isbn")
4642
* Book getBookWithIsbn(String isbn);
4743
* }
48-
* }
44+
*}
4945
* <p> A method return type <em>agrees</em> with the type returned by the
5046
* query if either:
5147
* <ul>
@@ -82,46 +78,9 @@
8278
*/
8379
@Target(METHOD)
8480
@Retention(RUNTIME)
85-
public @interface ReadQuery {
86-
81+
public @interface StaticQuery {
8782
/**
8883
* The query string in the Jakarta Persistence Query Language.
8984
*/
90-
String query();
91-
92-
/**
93-
* The lock mode type to use in query execution.
94-
* If a {@code lockMode} other than {@link LockModeType#NONE}
95-
* is specified, the query must be executed in a transaction
96-
* and the persistence context joined to the transaction.
97-
* @see jakarta.persistence.Query#setLockMode
98-
*/
99-
LockModeType lockMode() default LockModeType.NONE;
100-
101-
/**
102-
* The {@linkplain CacheStoreMode cache store mode} to use.
103-
* @see jakarta.persistence.Query#setCacheStoreMode
104-
*/
105-
CacheStoreMode cacheStoreMode() default CacheStoreMode.USE;
106-
107-
/**
108-
* The {@linkplain CacheRetrieveMode cache retrieve mode}
109-
* to use.
110-
* @see jakarta.persistence.Query#setCacheRetrieveMode
111-
*/
112-
CacheRetrieveMode cacheRetrieveMode() default CacheRetrieveMode.USE;
113-
114-
/**
115-
* A query timeout in milliseconds.
116-
* By default, there is no timeout.
117-
* @see jakarta.persistence.Query#setTimeout
118-
*/
119-
int timeout() default -1;
120-
121-
/**
122-
* Query properties and hints.
123-
* May include vendor-specific query hints.
124-
* @see jakarta.persistence.Query#setHint(String, Object)
125-
*/
126-
QueryHint[] hints() default {};
85+
String value();
12786
}

0 commit comments

Comments
 (0)