Skip to content

Commit 5ea40e2

Browse files
committed
HHH-14822 Support custom tenant identifier type
1 parent d367418 commit 5ea40e2

File tree

53 files changed

+418
-288
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+418
-288
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,21 @@ public interface SessionBuilder {
112112
* @param tenantIdentifier The tenant identifier.
113113
*
114114
* @return {@code this}, for method chaining
115+
* @deprecated Use {@link #tenantIdentifier(Object)} instead
115116
*/
117+
@Deprecated(forRemoval = true)
116118
SessionBuilder tenantIdentifier(String tenantIdentifier);
117119

120+
/**
121+
* Define the tenant identifier to be associated with the opened session.
122+
*
123+
* @param tenantIdentifier The tenant identifier.
124+
*
125+
* @return {@code this}, for method chaining
126+
* @since 6.4
127+
*/
128+
SessionBuilder tenantIdentifier(Object tenantIdentifier);
129+
118130
/**
119131
* Add one or more {@link SessionEventListener} instances to the list of
120132
* listeners for the new session to be built.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public interface SharedSessionContract extends QueryProducer, Closeable, Seriali
3131
*/
3232
String getTenantIdentifier();
3333

34+
/**
35+
* Obtain the tenant identifier associated with this session.
36+
*
37+
* @return The tenant identifier associated with this session, or {@code null}
38+
* @since 6.4
39+
*/
40+
Object getTenantIdentifierValue();
41+
3442
/**
3543
* End the session by releasing the JDBC connection and cleaning up.
3644
*

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ public interface StatelessSessionBuilder {
3636
* @param tenantIdentifier The tenant identifier.
3737
*
3838
* @return {@code this}, for method chaining
39+
* @deprecated Use {@link #tenantIdentifier(Object)} instead
3940
*/
41+
@Deprecated(forRemoval = true)
4042
StatelessSessionBuilder tenantIdentifier(String tenantIdentifier);
43+
44+
/**
45+
* Define the tenant identifier to be associated with the opened session.
46+
*
47+
* @param tenantIdentifier The tenant identifier.
48+
*
49+
* @return {@code this}, for method chaining
50+
* @since 6.4
51+
*/
52+
StatelessSessionBuilder tenantIdentifier(Object tenantIdentifier);
4153
}

hibernate-core/src/main/java/org/hibernate/binder/internal/TenantIdBinder.java

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,7 @@ public void bind(
5555
FILTER_NAME,
5656
"",
5757
singletonMap( PARAMETER_NAME, tenantIdType )
58-
) {
59-
// unfortunately the old APIs only accept String for a tenantId, so parse it
60-
@Override
61-
public Object processArgument(Object value) {
62-
if (value==null) {
63-
return null;
64-
}
65-
else if (value instanceof String) {
66-
return getParameterJdbcMapping( PARAMETER_NAME )
67-
.getJavaTypeDescriptor()
68-
.fromString((String) value);
69-
}
70-
else {
71-
return value;
72-
}
73-
}
74-
}
58+
)
7559
);
7660
}
7761
else {
@@ -89,27 +73,27 @@ else if (value instanceof String) {
8973
}
9074
}
9175
persistentClass.addFilter(
92-
FILTER_NAME,
93-
columnNameOrFormula(property)
94-
+ " = :"
95-
+ PARAMETER_NAME,
96-
true,
97-
emptyMap(),
98-
emptyMap()
99-
);
76+
FILTER_NAME,
77+
columnNameOrFormula( property )
78+
+ " = :"
79+
+ PARAMETER_NAME,
80+
true,
81+
emptyMap(),
82+
emptyMap()
83+
);
10084

101-
property.resetUpdateable(false);
102-
property.resetOptional(false);
85+
property.resetUpdateable( false );
86+
property.resetOptional( false );
10387
}
10488

10589
private String columnNameOrFormula(Property property) {
106-
if ( property.getColumnSpan()!=1 ) {
107-
throw new MappingException("@TenantId attribute must be mapped to a single column or formula");
90+
if ( property.getColumnSpan() != 1 ) {
91+
throw new MappingException( "@TenantId attribute must be mapped to a single column or formula" );
10892
}
109-
Selectable selectable = property.getSelectables().get(0);
93+
Selectable selectable = property.getSelectables().get( 0 );
11094
return selectable.isFormula()
111-
? ((Formula) selectable).getFormula()
112-
: ((Column) selectable).getName();
95+
? ( (Formula) selectable ).getFormula()
96+
: ( (Column) selectable ).getName();
11397
}
11498

11599
}

hibernate-core/src/main/java/org/hibernate/boot/SessionFactoryBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ public interface SessionFactoryBuilder {
377377
*
378378
* @see org.hibernate.cfg.AvailableSettings#MULTI_TENANT_IDENTIFIER_RESOLVER
379379
*/
380-
SessionFactoryBuilder applyCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver resolver);
380+
SessionFactoryBuilder applyCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver<?> resolver);
381381

382382
/**
383383
* If using the built-in JTA-based

hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryBuilderImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public SessionFactoryBuilder applyMultiTenancy(boolean enabled) {
262262
}
263263

264264
@Override
265-
public SessionFactoryBuilder applyCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver resolver) {
265+
public SessionFactoryBuilder applyCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver<?> resolver) {
266266
this.optionsBuilder.applyCurrentTenantIdentifierResolver( resolver );
267267
return this;
268268
}

hibernate-core/src/main/java/org/hibernate/boot/internal/SessionFactoryOptionsBuilder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public class SessionFactoryOptionsBuilder implements SessionFactoryOptions {
206206

207207
// multi-tenancy
208208
private boolean multiTenancyEnabled;
209-
private CurrentTenantIdentifierResolver currentTenantIdentifierResolver;
209+
private CurrentTenantIdentifierResolver<Object> currentTenantIdentifierResolver;
210210

211211
// Queries
212212
private SqmFunctionRegistry sqmFunctionRegistry;
@@ -1008,7 +1008,7 @@ public boolean isMultiTenancyEnabled() {
10081008
}
10091009

10101010
@Override
1011-
public CurrentTenantIdentifierResolver getCurrentTenantIdentifierResolver() {
1011+
public CurrentTenantIdentifierResolver<Object> getCurrentTenantIdentifierResolver() {
10121012
return currentTenantIdentifierResolver;
10131013
}
10141014

@@ -1390,8 +1390,9 @@ public void applyMultiTenancy(boolean enabled) {
13901390
this.multiTenancyEnabled = enabled;
13911391
}
13921392

1393-
public void applyCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver resolver) {
1394-
this.currentTenantIdentifierResolver = resolver;
1393+
public void applyCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver<?> resolver) {
1394+
//noinspection unchecked
1395+
this.currentTenantIdentifierResolver = (CurrentTenantIdentifierResolver<Object>) resolver;
13951396
}
13961397

13971398
public void enableNamedQueryCheckingOnStartup(boolean enabled) {

hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public T applyMultiTenancy(boolean enabled) {
210210
}
211211

212212
@Override
213-
public T applyCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver resolver) {
213+
public T applyCurrentTenantIdentifierResolver(CurrentTenantIdentifierResolver<?> resolver) {
214214
delegate.applyCurrentTenantIdentifierResolver( resolver );
215215
return getThis();
216216
}

hibernate-core/src/main/java/org/hibernate/boot/spi/AbstractDelegatingSessionFactoryOptions.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.hibernate.query.sqm.sql.SqmTranslatorFactory;
3636
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
3737
import org.hibernate.resource.jdbc.spi.StatementInspector;
38+
import org.hibernate.type.descriptor.java.JavaType;
3839
import org.hibernate.type.format.FormatMapper;
3940

4041
/**
@@ -213,10 +214,15 @@ public boolean isMultiTenancyEnabled() {
213214
}
214215

215216
@Override
216-
public CurrentTenantIdentifierResolver getCurrentTenantIdentifierResolver() {
217+
public CurrentTenantIdentifierResolver<Object> getCurrentTenantIdentifierResolver() {
217218
return delegate.getCurrentTenantIdentifierResolver();
218219
}
219220

221+
@Override
222+
public JavaType<Object> getDefaultTenantIdentifierJavaType() {
223+
return delegate.getDefaultTenantIdentifierJavaType();
224+
}
225+
220226
@Override
221227
public boolean isJtaTrackByThread() {
222228
return delegate.isJtaTrackByThread();

hibernate-core/src/main/java/org/hibernate/boot/spi/SessionFactoryOptions.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.hibernate.resource.jdbc.spi.PhysicalConnectionHandlingMode;
3333
import org.hibernate.resource.jdbc.spi.StatementInspector;
3434
import org.hibernate.stat.Statistics;
35+
import org.hibernate.type.descriptor.java.JavaType;
36+
import org.hibernate.type.descriptor.java.ObjectJavaType;
3537
import org.hibernate.type.format.FormatMapper;
3638

3739
/**
@@ -154,7 +156,7 @@ default boolean isAllowRefreshDetachedEntity() {
154156

155157
boolean isMultiTenancyEnabled();
156158

157-
CurrentTenantIdentifierResolver getCurrentTenantIdentifierResolver();
159+
CurrentTenantIdentifierResolver<Object> getCurrentTenantIdentifierResolver();
158160

159161
boolean isJtaTrackByThread();
160162

@@ -341,4 +343,14 @@ default boolean isCollectionsInDefaultFetchGroupEnabled() {
341343
*/
342344
@Incubating
343345
FormatMapper getXmlFormatMapper();
346+
347+
/**
348+
* The default tenant identifier java type to use, in case no explicit tenant identifier property is defined.
349+
*
350+
* @since 6.4
351+
*/
352+
@Incubating
353+
default JavaType<Object> getDefaultTenantIdentifierJavaType() {
354+
return ObjectJavaType.INSTANCE;
355+
}
344356
}

0 commit comments

Comments
 (0)