Skip to content

Commit 2fd8032

Browse files
committed
improve javadoc for session builders
1 parent 91e9fb6 commit 2fd8032

File tree

5 files changed

+96
-7
lines changed

5 files changed

+96
-7
lines changed

hibernate-core/src/main/java/org/hibernate/SessionBuilder.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,29 @@
1313
import org.hibernate.resource.jdbc.spi.StatementInspector;
1414

1515
/**
16-
* Allows creation of a new {@link Session} with specific options.
16+
* Allows creation of a new {@link Session} with specific options
17+
* overriding the defaults from the {@link SessionFactory}.
18+
* <pre>
19+
* try (var session =
20+
* sessionFactory.withOptions()
21+
* .tenantIdentifier(tenantId)
22+
* .initialCacheMode(CacheMode.PUT)
23+
* .flushMode(FlushMode.COMMIT)
24+
* .interceptor(new Interceptor() {
25+
* &#64;Override
26+
* public void preFlush(Iterator&lt;Object&gt; entities) {
27+
* ...
28+
* }
29+
* })
30+
* .openSession()) {
31+
* ...
32+
* }
33+
* </pre>
1734
*
1835
* @author Steve Ebersole
1936
*
2037
* @see SessionFactory#withOptions()
38+
* @see SharedSessionBuilder
2139
*/
2240
public interface SessionBuilder extends CommonBuilder {
2341
/**

hibernate-core/src/main/java/org/hibernate/SharedSessionBuilder.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,39 @@
1313
import java.util.function.UnaryOperator;
1414

1515
/**
16-
* Specialized {@link SessionBuilder} with access to stuff from another session.
16+
* Allows creation of a child {@link Session} which shares some options with
17+
* another pre-existing parent session. Each session has its own isolated
18+
* persistence context, and entity instances must not be shared between
19+
* parent and child sessions.
20+
* <p>
21+
* When {@linkplain Transaction resource-local} transaction management is used:
22+
* <ul>
23+
* <li>by default, each session executes with its own dedicated JDBC connection
24+
* and therefore has its own isolated transaction, but
25+
* <li>calling the {@link #connection()} method specifies that the connection,
26+
* and therefore also the JDBC transaction, should be shared from parent
27+
* to child.
28+
* </ul>
29+
* <p>
30+
* <pre>
31+
* try (var childSession
32+
* = session.sessionWithOptions()
33+
* .connection() // share the JDBC connection
34+
* .cacheMode(CacheMode.IGNORE)
35+
* .openSession()) {
36+
* ...
37+
* }
38+
* </pre>
39+
* <p>
40+
* On the other hand, when JTA transaction management is used, all sessions
41+
* execute within the same transaction. Typically, connection sharing is
42+
* handled automatically by the JTA-enabled {@link javax.sql.DataSource}.
1743
*
1844
* @author Steve Ebersole
1945
*
2046
* @see Session#sessionWithOptions()
47+
* @see StatelessSession#sessionWithOptions()
48+
* @see SessionBuilder
2149
*/
2250
public interface SharedSessionBuilder extends SessionBuilder, CommonSharedBuilder {
2351

hibernate-core/src/main/java/org/hibernate/SharedStatelessSessionBuilder.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,35 @@
1111
import java.util.function.UnaryOperator;
1212

1313
/**
14-
* Allows for creation of a {@linkplain StatelessSession stateless session} sharing the
15-
* underpinnings of another {@linkplain Session stateful} or {@linkplain StatelessSession stateless}
16-
* session.
14+
* Allows creation of a child {@link StatelessSession} which shares some options
15+
* with another pre-existing parent session.
16+
* <p>
17+
* When {@linkplain Transaction resource-local} transaction management is used:
18+
* <ul>
19+
* <li>by default, each session executes with its own dedicated JDBC connection
20+
* and therefore has its own isolated transaction, but
21+
* <li>calling the {@link #connection()} method specifies that the connection,
22+
* and therefore also the JDBC transaction, should be shared from parent
23+
* to child.
24+
* </ul>
25+
* <p>
26+
* <pre>
27+
* try (var statelessSession
28+
* = session.statelessWithOptions()
29+
* .connection() // share the JDBC connection
30+
* .cacheMode(CacheMode.IGNORE)
31+
* .openStatelessSession()) {
32+
* ...
33+
* }
34+
* </pre>
35+
* <p>
36+
* On the other hand, when JTA transaction management is used, all sessions
37+
* execute within the same transaction. Typically, connection sharing is
38+
* handled automatically by the JTA-enabled {@link javax.sql.DataSource}.
1739
*
1840
* @see Session#statelessWithOptions()
1941
* @see StatelessSession#statelessWithOptions()
42+
* @see SharedSessionBuilder
2043
*
2144
* @since 7.2
2245
*

hibernate-core/src/main/java/org/hibernate/StatelessSessionBuilder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import org.hibernate.resource.jdbc.spi.StatementInspector;
1313

1414
/**
15-
* Allows creation of a new {@link StatelessSession} with specific options.
15+
* Allows creation of a new {@link StatelessSession} with specific options
16+
* overriding the defaults from the {@link SessionFactory}.
1617
*
1718
* @author Steve Ebersole
1819
*
1920
* @see SessionFactory#withStatelessOptions()
21+
* @see SharedStatelessSessionBuilder
2022
*/
2123
public interface StatelessSessionBuilder extends CommonBuilder {
2224
/**

hibernate-core/src/main/java/org/hibernate/engine/creation/CommonBuilder.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,15 @@ public interface CommonBuilder {
124124
CommonBuilder noStatementInspector();
125125

126126
/**
127-
* Define the tenant identifier to be associated with the opened session.
127+
* Specify the tenant identifier to be associated with the opened session.
128+
* <pre>
129+
* try (var session =
130+
* sessionFactory.withOptions()
131+
* .tenantIdentifier(tenantId)
132+
* .openSession()) {
133+
* ...
134+
* }
135+
* </pre>
128136
*
129137
* @param tenantIdentifier The tenant identifier.
130138
*
@@ -160,6 +168,16 @@ public interface CommonBuilder {
160168
* possible to structure data access code in a way which eliminates
161169
* this possibility.
162170
* <p>
171+
* <pre>
172+
* try (var readOnlySession =
173+
* sessionFactory.withOptions()
174+
* .readOnly(true)
175+
* .initialCacheMode(CacheMode.IGNORE)
176+
* .openSession()) {
177+
* ...
178+
* }
179+
* </pre>
180+
* <p>
163181
* If a session is created in read-only mode, then it cannot be
164182
* changed to read-write mode, and any call to
165183
* {@link Session#setDefaultReadOnly(boolean)} with fail. On the

0 commit comments

Comments
 (0)