Skip to content

Commit d7aefc5

Browse files
use Hibernate's short-name strategy mechanism to avoid exposing implementation details
1 parent 23ad2b4 commit d7aefc5

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
hibernate.dialect=com.mongodb.hibernate.dialect.MongoDialect
2-
hibernate.connection.provider_class=com.mongodb.hibernate.jdbc.MongoConnectionProvider
1+
hibernate.dialect=mongodb
2+
hibernate.connection.provider_class=mongodb-connection-provider
33
jakarta.persistence.jdbc.url=mongodb://localhost/mongo-hibernate-test?directConnection=false
44
hibernate.query.plan_cache_enabled=false #make tests more isolated from each other

src/main/java/com/mongodb/hibernate/internal/MongoConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ private MongoConstants() {}
2929
public static final String MONGO_DBMS_NAME = "MongoDB";
3030
public static final String MONGO_JDBC_DRIVER_NAME = "MongoDB Java Driver JDBC Adapter";
3131
public static final String ID_FIELD_NAME = "_id";
32+
33+
public static final String MONGO_DIALECT_SHORT_NAME = "mongodb";
34+
public static final String MONGO_CONNECTION_PROVIDER_SHORT_NAME = "mongodb-connection-provider";
3235
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2025-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.hibernate.internal.extension;
18+
19+
import static com.mongodb.hibernate.internal.MongoConstants.MONGO_CONNECTION_PROVIDER_SHORT_NAME;
20+
import static com.mongodb.hibernate.internal.MongoConstants.MONGO_DIALECT_SHORT_NAME;
21+
22+
import com.mongodb.hibernate.dialect.MongoDialect;
23+
import com.mongodb.hibernate.jdbc.MongoConnectionProvider;
24+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
25+
import org.hibernate.boot.registry.selector.spi.StrategySelector;
26+
import org.hibernate.dialect.Dialect;
27+
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
28+
import org.hibernate.service.spi.ServiceContributor;
29+
30+
public final class MongoStrategyContributor implements ServiceContributor {
31+
32+
@Override
33+
public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder) {
34+
var selector = serviceRegistryBuilder.getBootstrapServiceRegistry().requireService(StrategySelector.class);
35+
selector.registerStrategyImplementor(Dialect.class, MONGO_DIALECT_SHORT_NAME, MongoDialect.class);
36+
selector.registerStrategyImplementor(
37+
ConnectionProvider.class, MONGO_CONNECTION_PROVIDER_SHORT_NAME, MongoConnectionProvider.class);
38+
}
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
com.mongodb.hibernate.internal.extension.service.StandardServiceRegistryScopedState$ServiceContributor
2+
com.mongodb.hibernate.internal.extension.MongoStrategyContributor
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
hibernate.dialect=com.mongodb.hibernate.dialect.MongoDialect
2-
hibernate.connection.provider_class=com.mongodb.hibernate.jdbc.MongoConnectionProvider
1+
hibernate.dialect=mongodb
2+
hibernate.connection.provider_class=mongodb-connection-provider
33
hibernate.boot.allow_jdbc_metadata_access=false
44
jakarta.persistence.jdbc.url=mongodb://host/mongo-hibernate-test?directConnection=false

0 commit comments

Comments
 (0)