Interface SessionBuilder
- All Superinterfaces:
CommonBuilder
- All Known Subinterfaces:
SessionBuilderImplementor, SharedSessionBuilder, SharedSessionBuilderImplementor
- All Known Implementing Classes:
AbstractDelegatingSessionBuilder, AbstractDelegatingSessionBuilderImplementor, AbstractDelegatingSharedSessionBuilder
Allows creation of a new Session with specific options
overriding the defaults from the SessionFactory.
try (var session = sessionFactory.withOptions()
.tenantIdentifier(tenantId)
.initialCacheMode(CacheMode.PUT)
.flushMode(FlushMode.COMMIT)
.interceptor(new Interceptor() {
@Override
public void preFlush(Iterator<Object> entities) {
...
}
})
.openSession()) {
...
}
}
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionautoClear(boolean autoClear) Should the session be automatically cleared on a failed transaction?autoClose(boolean autoClose) Should the session be automatically closed after transaction completion?autoJoinTransactions(boolean autoJoinTransactions) Should the session built automatically join in any ongoing JTA transactions.Remove all listeners intended for the built session currently held here, including any auto-apply ones; in other words, start with a clean slate.connection(Connection connection) Adds a specific connection to the session options.connectionHandling(ConnectionAcquisitionMode acquisitionMode, ConnectionReleaseMode releaseMode) Specifies the connection handling modes for the session.Deprecated.defaultBatchFetchSize(int defaultBatchFetchSize) Specify the default batch fetch size for the session.eventListeners(SessionEventListener... listeners) Add one or moreSessionEventListenerinstances to the list of listeners for the new session to be built.Specify the initialFlushModeto use for the opened SessionidentifierRollback(boolean identifierRollback) Enable identifier rollback after entity removal for the session.initialCacheMode(CacheMode cacheMode) Specify the initialCacheModefor the session.interceptor(Interceptor interceptor) Adds a specific interceptor to the session options.jdbcTimeZone(TimeZone timeZone) Specify the JDBC time zone for the session.Specifies that noInterceptorshould be used.Specifies that no session-scoped interceptor should be used for the session.Signifies that no SQL statement inspector should be used.default Sessionopen()Open the session using the specified options.Open the session using the specified options.readOnly(boolean readOnly) Specify a read-only mode for the session.statementInspector(UnaryOperator<String> operator) Applies the given statement inspection function to the session.statementInspector(StatementInspector statementInspector) Deprecated.This operation exposes the SPI typeStatementInspectorand is therefore a layer-breaker.subselectFetchEnabled(boolean subselectFetchEnabled) Specify whether subselect fetching is enabled for the session.tenantIdentifier(Object tenantIdentifier) Define the tenant identifier to be associated with the opened session.tenantIdentifier(String tenantIdentifier) Deprecated, for removal: This API element is subject to removal in a future version.UsetenantIdentifier(Object)instead
-
Method Details
-
openSession
-
open
Description copied from interface:CommonBuilderOpen the session using the specified options.- Specified by:
openin interfaceCommonBuilder
-
interceptor
Description copied from interface:CommonBuilderAdds a specific interceptor to the session options.- Specified by:
interceptorin interfaceCommonBuilder- Parameters:
interceptor- The interceptor to use.- Returns:
this, for method chaining
-
noInterceptor
SessionBuilder noInterceptor()Description copied from interface:CommonBuilderSpecifies that noInterceptorshould be used. This indicates to ignore both (if either) the interceptor and session-scoped interceptor associated with theSessionFactory- Specified by:
noInterceptorin interfaceCommonBuilder- Returns:
this, for method chaining
-
noSessionInterceptorCreation
SessionBuilder noSessionInterceptorCreation()Description copied from interface:CommonBuilderSpecifies that no session-scoped interceptor should be used for the session. If theSessionFactoryhas a configured interceptor, it will still be used.- Specified by:
noSessionInterceptorCreationin interfaceCommonBuilder- Returns:
this, for method chaining- See Also:
-
noStatementInspector
SessionBuilder noStatementInspector()Description copied from interface:CommonBuilderSignifies that no SQL statement inspector should be used.
By default, if no inspector is explicitly specified, the inspector associated with the
SessionFactory, if one, is inherited by the new session.- Specified by:
noStatementInspectorin interfaceCommonBuilder- Returns:
this, for method chaining
-
statementInspector
Description copied from interface:CommonBuilderApplies the given statement inspection function to the session.- Specified by:
statementInspectorin interfaceCommonBuilder- Parameters:
operator- An operator which accepts a SQL string, returning a processed SQL string to be used by Hibernate instead. The operator may simply (and usually) return the original SQL.- Returns:
this, for method chaining
-
statementInspector
Deprecated.This operation exposes the SPI typeStatementInspectorand is therefore a layer-breaker. UsestatementInspector(UnaryOperator)instead.Applies the givenStatementInspectorto the session.- Parameters:
statementInspector- TheStatementInspectorto use.- Returns:
this, for method chaining
-
connection
Adds a specific connection to the session options.- Specified by:
connectionin interfaceCommonBuilder- Parameters:
connection- The connection to use.- Returns:
this, for method chaining
-
connectionHandling
SessionBuilder connectionHandling(ConnectionAcquisitionMode acquisitionMode, ConnectionReleaseMode releaseMode) Specifies the connection handling modes for the session.Note that if
ConnectionAcquisitionMode.IMMEDIATELYis specified, then the release mode must beConnectionReleaseMode.ON_CLOSE.- Specified by:
connectionHandlingin interfaceCommonBuilder- Returns:
this, for method chaining- Since:
- 7.0
-
connectionHandlingMode
Deprecated.This operation exposes the SPI typePhysicalConnectionHandlingModeand is therefore a layer-breaker. UseconnectionHandling(ConnectionAcquisitionMode, ConnectionReleaseMode)instead.Specifies the connection handling mode.- Parameters:
mode- The connection handling mode to use.- Returns:
this, for method chaining
-
autoJoinTransactions
Should the session built automatically join in any ongoing JTA transactions.- Parameters:
autoJoinTransactions- Should JTA transactions be automatically joined- Returns:
this, for method chaining- See Also:
-
autoClear
Should the session be automatically cleared on a failed transaction?- Parameters:
autoClear- Whether the Session should be automatically cleared- Returns:
this, for method chaining
-
flushMode
Specify the initialFlushModeto use for the opened Session- Parameters:
flushMode- The initialFlushModeto use for the opened Session- Returns:
this, for method chaining- See Also:
-
tenantIdentifier
Deprecated, for removal: This API element is subject to removal in a future version.UsetenantIdentifier(Object)insteadDefine the tenant identifier to be associated with the opened session.- Parameters:
tenantIdentifier- The tenant identifier.- Returns:
this, for method chaining
-
tenantIdentifier
Define the tenant identifier to be associated with the opened session.- Specified by:
tenantIdentifierin interfaceCommonBuilder- Parameters:
tenantIdentifier- The tenant identifier.- Returns:
this, for method chaining- Since:
- 6.4
-
readOnly
Description copied from interface:CommonBuilderSpecify a read-only mode for the session. If a session is created in read-only mode, then
Connection.setReadOnly(boolean)is called when a JDBC connection is obtained.Furthermore, if read/write replication is in use, then:
- a read-only session will connect to a read-only replica, but
- a non-read-only session will connect to a writable replica.
When read/write replication is in use, it's strongly recommended that the session be created with the initial cache-mode set to
CacheMode.GET, to avoid writing stale data read from a read-only replica to the second-level cache. Hibernate cannot possibly guarantee that data read from a read-only replica is up to date.When read/write replication is in use, it's possible that an item read from the second-level cache might refer to data which does not yet exist in the read-only replica. In this situation, an exception occurs when the association is fetched. To completely avoid this possibility, the initial cache-mode must be set to
CacheMode.IGNORE. However, it's also usually possible to structure data access code in a way which eliminates this possibility.try (var readOnlySession = sessionFactory.withOptions() .readOnly(true) .initialCacheMode(CacheMode.IGNORE) .openSession()) { ... }If a session is created in read-only mode, then it cannot be changed to read-write mode, and any call to
Session.setDefaultReadOnly(boolean)with fail. On the other hand, if a session is created in read-write mode, then it may later be switched to read-only mode, but all database access is directed to the writable replica.- Specified by:
readOnlyin interfaceCommonBuilder- Returns:
this, for method chaining- See Also:
-
initialCacheMode
Description copied from interface:CommonBuilderSpecify the initialCacheModefor the session.- Specified by:
initialCacheModein interfaceCommonBuilder- Returns:
this, for method chaining- See Also:
-
eventListeners
Add one or moreSessionEventListenerinstances to the list of listeners for the new session to be built.- Parameters:
listeners- The listeners to incorporate into the built Session- Returns:
this, for method chaining
-
clearEventListeners
SessionBuilder clearEventListeners()Remove all listeners intended for the built session currently held here, including any auto-apply ones; in other words, start with a clean slate.- Returns:
this, for method chaining
-
jdbcTimeZone
Specify the JDBC time zone for the session.- Specified by:
jdbcTimeZonein interfaceCommonBuilder- Returns:
this, for method chaining
-
autoClose
Should the session be automatically closed after transaction completion?- Parameters:
autoClose- Should the session be automatically closed- Returns:
this, for method chaining- See Also:
-
identifierRollback
Enable identifier rollback after entity removal for the session.- Returns:
this, for method chaining- Since:
- 7.0
- See Also:
-
defaultBatchFetchSize
Specify the default batch fetch size for the session.- Returns:
this, for method chaining- Since:
- 7.2
- See Also:
-
subselectFetchEnabled
Specify whether subselect fetching is enabled for the session.- Returns:
this, for method chaining- Since:
- 7.2
- See Also:
-
PhysicalConnectionHandlingModeand is therefore a layer-breaker.