Skip to content

use Hibernate's short-name strategy mechanism to avoid exposing implementation details #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/integrationTest/resources/hibernate.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
hibernate.dialect=com.mongodb.hibernate.dialect.MongoDialect
hibernate.connection.provider_class=com.mongodb.hibernate.jdbc.MongoConnectionProvider
hibernate.dialect=mongodb
hibernate.connection.provider_class=mongodb-connection-provider
jakarta.persistence.jdbc.url=mongodb://localhost/mongo-hibernate-test?directConnection=false
hibernate.query.plan_cache_enabled=false #make tests more isolated from each other
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ private MongoConstants() {}
public static final String MONGO_DBMS_NAME = "MongoDB";
public static final String MONGO_JDBC_DRIVER_NAME = "MongoDB Java Driver JDBC Adapter";
public static final String ID_FIELD_NAME = "_id";

public static final String MONGO_DIALECT_SHORT_NAME = "mongodb";
public static final String MONGO_CONNECTION_PROVIDER_SHORT_NAME = "mongodb-connection-provider";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2025-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.mongodb.hibernate.internal.extension;

import static com.mongodb.hibernate.internal.MongoConstants.MONGO_CONNECTION_PROVIDER_SHORT_NAME;
import static com.mongodb.hibernate.internal.MongoConstants.MONGO_DIALECT_SHORT_NAME;

import com.mongodb.hibernate.dialect.MongoDialect;
import com.mongodb.hibernate.jdbc.MongoConnectionProvider;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.spi.ServiceContributor;

public final class MongoStrategyContributor implements ServiceContributor {

@Override
public void contribute(StandardServiceRegistryBuilder serviceRegistryBuilder) {
var selector = serviceRegistryBuilder.getBootstrapServiceRegistry().requireService(StrategySelector.class);
selector.registerStrategyImplementor(Dialect.class, MONGO_DIALECT_SHORT_NAME, MongoDialect.class);
selector.registerStrategyImplementor(
ConnectionProvider.class, MONGO_CONNECTION_PROVIDER_SHORT_NAME, MongoConnectionProvider.class);
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
com.mongodb.hibernate.internal.extension.service.StandardServiceRegistryScopedState$ServiceContributor
com.mongodb.hibernate.internal.extension.MongoStrategyContributor
4 changes: 2 additions & 2 deletions src/test/resources/hibernate.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
hibernate.dialect=com.mongodb.hibernate.dialect.MongoDialect
hibernate.connection.provider_class=com.mongodb.hibernate.jdbc.MongoConnectionProvider
hibernate.dialect=mongodb
hibernate.connection.provider_class=mongodb-connection-provider
hibernate.boot.allow_jdbc_metadata_access=false
jakarta.persistence.jdbc.url=mongodb://host/mongo-hibernate-test?directConnection=false