Skip to content

Commit 0a0de45

Browse files
committed
sort out two layer-breaking operations on SessionBuilder
1 parent 88eb5fd commit 0a0de45

File tree

8 files changed

+155
-65
lines changed

8 files changed

+155
-65
lines changed

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.sql.Connection;
88
import java.util.TimeZone;
9+
import java.util.function.Function;
910

1011
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
1112
import org.hibernate.resource.jdbc.spi.StatementInspector;
@@ -47,13 +48,32 @@ public interface SessionBuilder {
4748
*/
4849
SessionBuilder noInterceptor();
4950

51+
/**
52+
* Applies the given statement inspection function to the session.
53+
*
54+
* @param statementInspector A function which accepts a SQL string,
55+
* returning a possibly-processed SQL string
56+
* to be used by Hibernate instead of the
57+
* given SQL.
58+
*
59+
* @return {@code this}, for method chaining
60+
*
61+
* @since 7.0
62+
*/
63+
SessionBuilder statementInspector(Function<String,String> statementInspector);
64+
5065
/**
5166
* Applies the given {@link StatementInspector} to the session.
5267
*
53-
* @param statementInspector The StatementInspector to use.
68+
* @param statementInspector The {@code StatementInspector} to use.
5469
*
5570
* @return {@code this}, for method chaining
71+
*
72+
* @deprecated This operation exposes the SPI type {@link StatementInspector}
73+
* and is therefore a layer-breaker. Use {@link #statementInspector(Function)}
74+
* instead.
5675
*/
76+
@Deprecated(since = "7.0")
5777
SessionBuilder statementInspector(StatementInspector statementInspector);
5878

5979
/**
@@ -66,13 +86,27 @@ public interface SessionBuilder {
6686
SessionBuilder connection(Connection connection);
6787

6888
/**
69-
* Signifies that the connection release mode from the original session
70-
* should be used to create the new session.
89+
* Specifies the connection handling modes for the session.
90+
*
91+
* @return {@code this}, for method chaining
92+
*
93+
* @since 7.0
94+
*/
95+
SessionBuilder connectionHandling(ConnectionAcquisitionMode acquisitionMode, ConnectionReleaseMode releaseMode);
96+
97+
/**
98+
* Specifies the {@linkplain PhysicalConnectionHandlingMode connection handling mode}.
7199
*
72100
* @param mode The connection handling mode to use.
73101
*
74102
* @return {@code this}, for method chaining
103+
*
104+
* @deprecated This operation exposes the SPI type
105+
* {@link PhysicalConnectionHandlingMode} and is therefore a layer-breaker.
106+
* Use {@link #connectionHandling(ConnectionAcquisitionMode, ConnectionReleaseMode)}
107+
* instead.
75108
*/
109+
@Deprecated(since = "7.0")
76110
SessionBuilder connectionHandlingMode(PhysicalConnectionHandlingMode mode);
77111

78112
/**

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.sql.Connection;
1111
import java.util.TimeZone;
12+
import java.util.function.Function;
1213

1314
/**
1415
* Specialized {@link SessionBuilder} with access to stuff from another session.
@@ -38,7 +39,7 @@ public interface SharedSessionBuilder extends SessionBuilder {
3839
*
3940
* @return {@code this}, for method chaining
4041
*
41-
* @deprecated use {@link #connectionHandlingMode} instead.
42+
* @deprecated use {@link #connectionHandling} instead.
4243
*/
4344
@Deprecated(since = "6.0")
4445
SharedSessionBuilder connectionReleaseMode();
@@ -71,12 +72,18 @@ public interface SharedSessionBuilder extends SessionBuilder {
7172
*/
7273
SharedSessionBuilder autoClose();
7374

74-
@Override
75+
@Override @Deprecated
7576
SharedSessionBuilder statementInspector(StatementInspector statementInspector);
7677

7778
@Override
79+
SessionBuilder statementInspector(Function<String, String> statementInspector);
80+
81+
@Override @Deprecated
7882
SharedSessionBuilder connectionHandlingMode(PhysicalConnectionHandlingMode mode);
7983

84+
@Override
85+
SharedSessionBuilder connectionHandling(ConnectionAcquisitionMode acquisitionMode, ConnectionReleaseMode releaseMode);
86+
8087
@Override
8188
SharedSessionBuilder autoClear(boolean autoClear);
8289

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package org.hibernate;
66

77
import java.sql.Connection;
8+
import java.util.function.Function;
89

910
import org.hibernate.resource.jdbc.spi.StatementInspector;
1011

@@ -54,11 +55,32 @@ public interface StatelessSessionBuilder {
5455
StatelessSessionBuilder tenantIdentifier(Object tenantIdentifier);
5556

5657
/**
57-
* Applies the given {@link StatementInspector} to the stateless session.
58+
* Applies the given statement inspection function to the session.
5859
*
59-
* @param statementInspector The StatementInspector to use.
60+
* @param statementInspector A function which accepts a SQL string,
61+
* returning a possibly-processed SQL string
62+
* to be used by Hibernate instead of the
63+
* given SQL.
6064
*
6165
* @return {@code this}, for method chaining
66+
*
67+
* @apiNote This operation exposes the SPI type
68+
* {@link StatementInspector}
69+
* and is therefore a layer-breaker.
70+
*/
71+
StatelessSessionBuilder statementInspector(Function<String,String> statementInspector);
72+
73+
/**
74+
* Applies the given {@link StatementInspector} to the session.
75+
*
76+
* @param statementInspector The {@code StatementInspector} to use.
77+
*
78+
* @return {@code this}, for method chaining
79+
*
80+
* @deprecated This operation exposes the SPI type{@link StatementInspector}
81+
* and is therefore a layer-breaker. Use {@link #statementInspector(Function)}
82+
* instead.
6283
*/
84+
@Deprecated(since = "7.0")
6385
StatelessSessionBuilder statementInspector(StatementInspector statementInspector);
6486
}

hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSessionBuilder.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
import java.sql.Connection;
88
import java.util.TimeZone;
9+
import java.util.function.Function;
910

11+
import org.hibernate.ConnectionAcquisitionMode;
12+
import org.hibernate.ConnectionReleaseMode;
1013
import org.hibernate.FlushMode;
1114
import org.hibernate.Interceptor;
1215
import org.hibernate.Session;
@@ -55,12 +58,18 @@ public SessionBuilder noInterceptor() {
5558
return this;
5659
}
5760

58-
@Override
61+
@Override @Deprecated
5962
public SessionBuilder statementInspector(StatementInspector statementInspector) {
6063
delegate.statementInspector( statementInspector );
6164
return this;
6265
}
6366

67+
@Override
68+
public SessionBuilder statementInspector(Function<String, String> statementInspector) {
69+
delegate.statementInspector( statementInspector );
70+
return this;
71+
}
72+
6473
@Override
6574
public SessionBuilder connection(Connection connection) {
6675
delegate.connection( connection );
@@ -109,12 +118,18 @@ public SessionBuilder jdbcTimeZone(TimeZone timeZone) {
109118
return this;
110119
}
111120

112-
@Override
121+
@Override @Deprecated
113122
public SessionBuilder connectionHandlingMode(PhysicalConnectionHandlingMode mode) {
114123
delegate.connectionHandlingMode( mode );
115124
return this;
116125
}
117126

127+
@Override
128+
public SessionBuilder connectionHandling(ConnectionAcquisitionMode acquisitionMode, ConnectionReleaseMode releaseMode) {
129+
delegate.connectionHandling( acquisitionMode, releaseMode );
130+
return this;
131+
}
132+
118133
@Override
119134
public SessionBuilder autoClear(boolean autoClear) {
120135
delegate.autoClear( autoClear );

hibernate-core/src/main/java/org/hibernate/engine/spi/AbstractDelegatingSharedSessionBuilder.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66

77
import java.sql.Connection;
88
import java.util.TimeZone;
9+
import java.util.function.Function;
910

11+
import org.hibernate.ConnectionAcquisitionMode;
12+
import org.hibernate.ConnectionReleaseMode;
1013
import org.hibernate.FlushMode;
1114
import org.hibernate.Interceptor;
1215
import org.hibernate.Session;
16+
import org.hibernate.SessionBuilder;
1317
import org.hibernate.SessionEventListener;
1418
import org.hibernate.SharedSessionBuilder;
1519
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
@@ -91,12 +95,18 @@ public SharedSessionBuilder noInterceptor() {
9195
return this;
9296
}
9397

94-
@Override
98+
@Override @Deprecated
9599
public SharedSessionBuilder statementInspector(StatementInspector statementInspector) {
96100
delegate.statementInspector( statementInspector );
97101
return this;
98102
}
99103

104+
@Override
105+
public SessionBuilder statementInspector(Function<String, String> statementInspector) {
106+
delegate.statementInspector( statementInspector );
107+
return this;
108+
}
109+
100110
@Override
101111
public SharedSessionBuilder connection(Connection connection) {
102112
delegate.connection( connection );
@@ -139,12 +149,18 @@ public SharedSessionBuilder clearEventListeners() {
139149
return this;
140150
}
141151

142-
@Override
152+
@Override @Deprecated
143153
public SharedSessionBuilder connectionHandlingMode(PhysicalConnectionHandlingMode mode) {
144154
delegate.connectionHandlingMode( mode );
145155
return this;
146156
}
147157

158+
@Override
159+
public SharedSessionBuilder connectionHandling(ConnectionAcquisitionMode acquisitionMode, ConnectionReleaseMode releaseMode) {
160+
delegate.connectionHandling( acquisitionMode, releaseMode );
161+
return this;
162+
}
163+
148164
@Override
149165
public SharedSessionBuilder autoClear(boolean autoClear) {
150166
delegate.autoClear( autoClear );

hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import javax.naming.StringRefAddr;
2626

2727
import jakarta.persistence.TypedQuery;
28+
import org.hibernate.ConnectionAcquisitionMode;
29+
import org.hibernate.ConnectionReleaseMode;
2830
import org.hibernate.CustomEntityDirtinessStrategy;
2931
import org.hibernate.EntityNameResolver;
3032
import org.hibernate.FlushMode;
@@ -1271,24 +1273,36 @@ public SessionBuilderImpl noInterceptor() {
12711273
return this;
12721274
}
12731275

1274-
@Override
1276+
@Override @Deprecated
12751277
public SessionBuilderImpl statementInspector(StatementInspector statementInspector) {
12761278
this.statementInspector = statementInspector;
12771279
return this;
12781280
}
12791281

1282+
@Override
1283+
public SessionBuilder statementInspector(Function<String, String> statementInspector) {
1284+
this.statementInspector = statementInspector::apply;
1285+
return this;
1286+
}
1287+
12801288
@Override
12811289
public SessionBuilderImpl connection(Connection connection) {
12821290
this.connection = connection;
12831291
return this;
12841292
}
12851293

1286-
@Override
1294+
@Override @Deprecated
12871295
public SessionBuilderImpl connectionHandlingMode(PhysicalConnectionHandlingMode connectionHandlingMode) {
12881296
this.connectionHandlingMode = connectionHandlingMode;
12891297
return this;
12901298
}
12911299

1300+
@Override
1301+
public SessionBuilder connectionHandling(ConnectionAcquisitionMode acquisitionMode, ConnectionReleaseMode releaseMode) {
1302+
this.connectionHandlingMode = PhysicalConnectionHandlingMode.interpret( acquisitionMode, releaseMode);
1303+
return this;
1304+
}
1305+
12921306
@Override
12931307
public SessionBuilderImpl autoJoinTransactions(boolean autoJoinTransactions) {
12941308
this.autoJoinTransactions = autoJoinTransactions;
@@ -1401,12 +1415,18 @@ public StatelessSessionBuilder tenantIdentifier(Object tenantIdentifier) {
14011415
return this;
14021416
}
14031417

1404-
@Override
1418+
@Override @Deprecated
14051419
public StatelessSessionBuilder statementInspector(StatementInspector statementInspector) {
14061420
this.statementInspector = statementInspector;
14071421
return this;
14081422
}
14091423

1424+
@Override
1425+
public StatelessSessionBuilder statementInspector(Function<String, String> statementInspector) {
1426+
this.statementInspector = statementInspector::apply;
1427+
return this;
1428+
}
1429+
14101430
@Override
14111431
public boolean shouldAutoJoinTransactions() {
14121432
return true;

0 commit comments

Comments
 (0)