Skip to content

Conversation

@stIncMale
Copy link
Member

@stIncMale stIncMale requested a review from vbabanin November 12, 2025 16:33
@stIncMale stIncMale self-assigned this Nov 12, 2025
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test classes should have the "Tests" suffix. This one had, but the name of the file in which it was declared was "Test".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was renamed to NativeBootstrappingIntegrationTests. The code was also simplified, taking into account that MongoExtension now disables fail points.

var item = new Item();
item.id = 1;
em.persist(item);
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only drive-by changes here to make the test code simpler.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test classes should have the "Tests" suffix.

Comment on lines +81 to +90
@Override
public void contribute(
AdditionalMappingContributions contributions,
InFlightMetadataCollector metadata,
ResourceStreamLocator resourceStreamLocator,
MetadataBuildingContext buildingContext) {
if (!(metadata.getDatabase().getDialect() instanceof MongoDialect)) {
// avoid interfering with bootstrapping not related to the MongoDB Extension for Hibernate ORM
return;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix.

* limitations under the License.
*/

package com.mongodb.hibernate.internal.extension.service;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the program elements we have represent part of the extension. The extension in the package name does not make sense, though originally I thought it would make sense.

private @Nullable StandardServiceRegistryScopedState standardServiceRegistryScopedState;
private transient @Nullable MongoClient mongoClient;

public MongoConnectionProvider() {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made the constructor used by Hibernate ORM explicit, like we do in other places.

@stIncMale stIncMale changed the title Make sure our extension does not interfere when bootstrapping is not related to it Make sure our extension does not interfere when bootstrapping unrelated to it Nov 12, 2025
*/

package com.mongodb.hibernate.internal.extension;
package com.mongodb.hibernate.internal.boot;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Most of the program elements we have represent part of the extension. The extension in the package name does not make sense, though originally I thought it would make sense.
  2. org.hibernate.boot.spi.AdditionalMappingContributor is in the boot package, which is why I put MongoAdditionalMappingContributor to the boot package.

@stIncMale stIncMale marked this pull request as ready for review November 13, 2025 00:54
@stIncMale stIncMale requested a review from a team as a code owner November 13, 2025 00:54
@stIncMale stIncMale changed the title Make sure our extension does not interfere when bootstrapping unrelated to it Make sure our extension does not interfere when bootstrapping is unrelated to it Nov 18, 2025
@stIncMale stIncMale requested a review from vbabanin November 19, 2025 08:11
@vbabanin
Copy link
Member

vbabanin commented Nov 19, 2025

As discussed offline, we could use also check if dialect is MongoDB dialect in our ServiceContributor via settings, so that if the user makes a mistake and uses MongoConnectionProvider with non MongoDB Dialect we will fail (because it requires StandardServiceRegistryScopedState) with a user-friendly message.

@stIncMale
Copy link
Member Author

As discussed offline, we could use also check if dialect is MongoDB dialect in our ServiceContributor via settings, so that if the user makes a mistake and uses MongoConnectionProvider with non MongoDB Dialect we will fail (because it requires StandardServiceRegistryScopedState) with a user-friendly message.

Done in f7dbe58.

* MongoDB Extension for Hibernate ORM.
*/
@Test
void testMongoAdditionalMappingContributorIsSkipped() {
Copy link
Member Author

@stIncMale stIncMale Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new test fails without the changes done to MongoAdditionalMappingContributor.contribute.

Previously in this PR this test was in PostgreSQLBootstrappingIntegrationTests, but I realized that it can be made a unit test without any dependency on the PostgreSQL driver.


class NativeBootstrappingTests {
@Test
void testMongoDialectNotPluggedIn() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note how there is no testMongoConnectionProviderNotPluggedIn test in NativeBootstrappingTests. That is because it's impossible to write such a test without actually connecting to a DBMS, and that DBMS would have to be not MongoDB, as the connection provider cannot be ours.

However, StandardServiceRegistryScopedStateTests has both testMongoDialectNotPluggedIn and testMongoConnectionProviderNotPluggedIn.

.buildMetadata(new StandardServiceRegistryBuilder()
.applySettings(settings)
.build())
.buildSessionFactory();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A drive-by simplification.

Map.of(JAKARTA_JDBC_URL, "jdbc:postgresql://localhost/test"))
.close());
assertThrows(
ServiceException.class, () -> buildSessionFactory(Map.of(JAKARTA_JDBC_URL, "jdbc:postgresql://host/"))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the ULR such that there is obviously no chance in practice to successfully connect using it.

public StandardServiceRegistryScopedState initiateService(
Map<String, Object> configurationValues, ServiceRegistryImplementor serviceRegistry) {
checkMongoDialectIsPluggedIn(configurationValues, serviceRegistry);
checkMongoConnectionProviderIsPluggedIn(configurationValues);
Copy link
Member Author

@stIncMale stIncMale Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally I wanted to put the checkMongoConnectionProviderIsPluggedIn check into MongoDialect. Unfortunately, there is no way, as far as I can see, to check configuration properties from MongoDialect. The closest to the dialect place where we can do that is the constructor of AbstractMqlTranslator. However, that constructor requires StandardServiceRegistryScopedState, which is why I put the checkMongoConnectionProviderIsPluggedIn check here, close to the checkMongoDialectIsPluggedIn check, which should be here anyway.

Comment on lines +63 to +67
return new MetadataSources()
.buildMetadata(new StandardServiceRegistryBuilder()
.addService(MongoConfigurationContributor.class, mongoConfigurationContributor)
.build())
.getMetadataBuilder()
.build()
.getSessionFactoryBuilder()
.build();
.buildSessionFactory();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A drive-by simplification.

HIBERNATE-135
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants