Skip to content

Commit

Permalink
change as per Valentin's excellent code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanQingyangXu committed Nov 8, 2024
1 parent 452c559 commit 2124619
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 67 deletions.
42 changes: 17 additions & 25 deletions src/main/java/com/mongodb/hibernate/dialect/MongoDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,35 @@

import org.hibernate.dialect.DatabaseVersion;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
import org.jspecify.annotations.NullMarked;

/**
* A MongoDB {@linkplain Dialect} for version 6.0 and above.
* A MongoDB {@link Dialect} for {@linkplain #getMinimumSupportedVersion() version 6.0 and above}.
*
* <p>Usually Hibernate dialect represents some SQL RDBMS and speaks SQL with vendor-specific difference. MongoDB is a
* document DB and speaks <i>MQL</i> (MongoDB Query Language) in JSON format, but it is still possible to integrate with
* Hibernate seamlessly by creating a JDBC adaptor on top of MongoDB's Java client library.
*
* <p>Some MongoDB-specific customization examples include:
*
* <ul>
* <li>MQL translation extension point
* <li>SQL {@linkplain java.sql.Types#ARRAY ARRAY} and {@linkplain java.sql.Types#STRUCT STRUCT} extension points to
* support MongoDB's embedding array and document
* <li>MQL parameterization customization
* </ul>
* document DB and speaks <i>MQL</i> (MongoDB Query Language), but it is still possible to integrate with Hibernate by
* creating a JDBC adaptor on top of MongoDB's Java Driver.
*/
public class MongoDialect extends Dialect {
public static final int MINIMUM_MONGODB_MAJOR_VERSION_SUPPORTED = 6;
public static final int MINIMUM_MONGODB_MINOR_VERSION_SUPPORTED = 0;
@NullMarked
public final class MongoDialect extends Dialect {

private static final DatabaseVersion MINIMUM_VERSION =
DatabaseVersion.make(MINIMUM_MONGODB_MAJOR_VERSION_SUPPORTED, MINIMUM_MONGODB_MINOR_VERSION_SUPPORTED);
/** Default constructor used when no version info is available. */
public MongoDialect() {
super((DatabaseVersion) null);
}

/**
* Default constructor used when no version info is available.
* Constructor used when actual metadata info is available.
*
* <p>Note that {@link Dialect} abstract class has two overloaded constructors accepting sole parameter, and only
* one accepts {@code null} value. Explicitly downcast to make the intention clear.
* @param info MongoDB metadata
*/
public MongoDialect() {
super((DatabaseVersion) null);
public MongoDialect(DialectResolutionInfo info) {
super(info);
}

/** {@inheritDoc} */
@Override
protected DatabaseVersion getMinimumSupportedVersion() {
return MINIMUM_VERSION;
public DatabaseVersion getMinimumSupportedVersion() {
return DatabaseVersion.make(6);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
* limitations under the License.
*/

package com.mongodb.hibernate;
package com.mongodb.hibernate.internal;

import org.jspecify.annotations.NullMarked;

/**
* A temporary marker exception to denote that the feature in question is in the scope of MongoDB dialect but has not
Expand All @@ -25,8 +27,11 @@
*
* <p>It is recommended to provide some message to explain when it will be implemented (e.g. JIRA ticket id is a good
* idea), but that is optional.
*
* <p>This class is not part of the public API and may be removed or changed at any time.
*/
public class NotYetImplementedException extends RuntimeException {
@NullMarked
public final class NotYetImplementedException extends RuntimeException {

/**
* Default constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.hibernate.NotYetImplementedException;
import com.mongodb.hibernate.internal.NotYetImplementedException;
import java.io.Serial;
import java.sql.Connection;
import java.util.Map;
import org.hibernate.HibernateException;
Expand All @@ -31,13 +32,14 @@
import org.hibernate.service.UnknownUnwrapTypeException;
import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.Stoppable;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/**
* MongoDB dialect's customized JDBC {@link ConnectionProvider} SPI implementation, whose class name could be provided
* using configuration property {@value org.hibernate.cfg.JdbcSettings#CONNECTION_PROVIDER}.
* {@linkplain com.mongodb.hibernate.dialect.MongoDialect MongoDB dialect}'s customized JDBC {@link ConnectionProvider}
* SPI implementation.
*
* <p>The following Hibernate JDBC properties will be relied upon by Hibernate's {@link Configurable} SPI mechanism:
* <p>{@link MongoConnectionProvider} uses the following Hibernate properties:
*
* <ul>
* <li>{@linkplain org.hibernate.cfg.JdbcSettings#JAKARTA_JDBC_URL jakarta.persistence.jdbc.url}
Expand All @@ -55,7 +57,11 @@
* @see JdbcSettings#JAKARTA_JDBC_PASSWORD
* @see <a href="https://www.mongodb.com/docs/manual/reference/connection-string/">connection string</a>
*/
public class MongoConnectionProvider implements ConnectionProvider, Configurable, Stoppable {
@NullMarked
public final class MongoConnectionProvider implements ConnectionProvider, Configurable, Stoppable {

@Serial
private static final long serialVersionUID = -1666024601592368426L;

private @Nullable MongoClient mongoClient;

Expand Down Expand Up @@ -99,12 +105,18 @@ public void configure(Map<String, Object> configValues) {
ConnectionString connectionString;
try {
connectionString = new ConnectionString((String) jdbcUrl);
} catch (IllegalArgumentException iae) {
throw new HibernateException("Invalid MongoDB connection string: " + jdbcUrl, iae);
} catch (RuntimeException e) {
throw new HibernateException(
String.format(
"Failed to create ConnectionString from configuration ('%s') with value ('%s')",
JAKARTA_JDBC_URL, jdbcUrl),
e);
}
var database = connectionString.getDatabase();
if (database == null) {
throw new HibernateException("Database must be provided in connection string: " + jdbcUrl);
throw new HibernateException(String.format(
"Database must be provided in ConnectionString from configuration ('%s') with value ('%s')",
JAKARTA_JDBC_URL, jdbcUrl));
}

var clientSettingsBuilder = MongoClientSettings.builder().applyConnectionString(connectionString);
Expand Down
32 changes: 0 additions & 32 deletions src/main/java/com/mongodb/hibernate/package-info.java

This file was deleted.

0 comments on commit 2124619

Please sign in to comment.