Skip to content

Commit cf0f2e4

Browse files
committed
improve javadoc for @view, @subselect, and @SQLSelect
1 parent d71a2e5 commit cf0f2e4

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

hibernate-core/src/main/java/org/hibernate/annotations/SQLSelect.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@
3434
* <li>the foreign key, in the case of a collection.
3535
* </ol>
3636
* <p>
37+
* If the {@linkplain #querySpaces tables which affect the query results}
38+
* are specified, then changes to those tables will be flushed before
39+
* execution of the query.
40+
* <p>
41+
* For example:
42+
* <pre>
43+
* &#64;SQLSelect(sql = """
44+
* SELECT id, created, text
45+
* FROM records
46+
* WHERE id = ? and deleted is null
47+
* """
48+
* querySpaces = "records")
49+
* &#64;Entity
50+
* public class Record { ... }
51+
* </pre>
52+
* <p>
3753
* Optionally, an explicit {@linkplain #resultSetMapping result set mapping}
3854
* may be specified. It should have:
3955
* <ol>

hibernate-core/src/main/java/org/hibernate/annotations/Subselect.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,49 @@
1111
import static java.lang.annotation.RetentionPolicy.RUNTIME;
1212

1313
/**
14-
* Maps an immutable and read-only entity to a given SQL {@code select} expression.
14+
* Maps an {@linkplain Immutable immutable} and read-only entity to a given
15+
* SQL {@code select} expression.
1516
* <p>
16-
* This is an alternative to defining a database view and mapping the entity to
17-
* the view using the {@link jakarta.persistence.Table @Table} annotation.
17+
* For example:
18+
* <pre>
19+
* &#64;Immutable &#64;Entity
20+
* &#64;Subselect("""
21+
* select type, sum(amount) as total, avg(amount) as average
22+
* from details
23+
* group by type
24+
* """)
25+
* &#64;Synchronize("details")
26+
* public class Summary {
27+
* &#64;Id String type;
28+
* Double total;
29+
* Double average;
30+
* }
31+
* </pre>
32+
* <p>
33+
* This is an alternative to defining a {@linkplain View view} and mapping
34+
* the entity to the view using the {@link jakarta.persistence.Table @Table}
35+
* annotation.
36+
* <p>
37+
* It's possible to have an entity class which maps a table, and another
38+
* entity which is defined by a {@code @Subselect} involving the same table.
39+
* In this case, a stateful session is vulnerable to data aliasing effects,
40+
* and it's the responsibility of client code to ensure that changes to the
41+
* first entity are flushed to the database before reading the same data via
42+
* the second entity. The {@link Synchronize @Synchronize} annotation can
43+
* help alleviate this problem, but it's an incomplete solution. We therefore
44+
* recommend the use of {@linkplain org.hibernate.StatelessSession stateless
45+
* sessions} in this situation.
1846
*
1947
* @see Synchronize
48+
* @see View
2049
*
2150
* @author Sharath Reddy
2251
*/
2352
@Target(TYPE)
2453
@Retention(RUNTIME)
2554
public @interface Subselect {
2655
/**
27-
* The query.
56+
* The subquery, written in native SQL.
2857
*/
2958
String value();
3059
}

hibernate-core/src/main/java/org/hibernate/annotations/Synchronize.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* Ordinarily, Hibernate knows the tables containing the state of an
2626
* entity or collection. This annotation might be necessary if:
2727
* <ul>
28-
* <li>an entity or collection maps a database view,
28+
* <li>an entity or collection maps a database {@linkplain View view},
2929
* <li>an entity or collection is persisted using handwritten SQL,
3030
* that is, using {@link SQLSelect @SQLSelect} and friends, or
3131
* <li>an entity is mapped using {@link Subselect @Subselect}.
@@ -41,6 +41,9 @@
4141
* @author Sharath Reddy
4242
*
4343
* @see org.hibernate.query.SynchronizeableQuery
44+
* @see View
45+
* @see Subselect
46+
* @see SQLSelect
4447
*/
4548
@Target({TYPE, FIELD, METHOD})
4649
@Retention(RUNTIME)

hibernate-core/src/main/java/org/hibernate/annotations/View.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,24 @@
2626
* <pre>
2727
* &#64;Immutable &#64;Entity
2828
* &#64;Table(name="summary")
29-
* &#64;View(query="select type, sum(amount) as total, avg(amount) as average from details group by type")
29+
* &#64;View(query="""
30+
* select type, sum(amount) as total, avg(amount) as average
31+
* from details
32+
* group by type
33+
* """)
3034
* &#64;Synchronize("details")
3135
* public class Summary {
3236
* &#64;Id String type;
3337
* Double total;
3438
* Double average;
3539
* }
3640
* </pre>
37-
* <p>
3841
* results in the following generated DDL:
3942
* <pre>
4043
* create view summary
41-
* as select type, sum(amount) as total, avg(amount) as average from details group by type
44+
* as select type, sum(amount) as total, avg(amount) as average
45+
* from details
46+
* group by type
4247
* </pre>
4348
* <p>
4449
* If a view is not updatable, we recommend annotating the
@@ -47,7 +52,7 @@
4752
* It's possible to have an entity class which maps a table,
4853
* and another entity which maps a view defined as a query
4954
* against that table. In this case, a stateful session is
50-
* vulnerable to data aliasing effects, and it is the
55+
* vulnerable to data aliasing effects, and it's the
5156
* responsibility of client code to ensure that changes to
5257
* the first entity are flushed to the database before
5358
* reading the same data via the second entity. The
@@ -61,6 +66,8 @@
6166
* @since 6.3
6267
*
6368
* @author Gavin King
69+
*
70+
* @see Synchronize
6471
*/
6572
@Incubating
6673
@Target(TYPE)

0 commit comments

Comments
 (0)