-
Notifications
You must be signed in to change notification settings - Fork 14
Add support for ObjectId
#70
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8ac53b2
Add support for `ObjectId`
stIncMale bff3df1
Add `ObjectIdFieldTypeIntegrationTests.getById`
stIncMale fdefbd9
Test `MongoPreparedStatement.setObject`
stIncMale b9346b8
Test `MongoResultSet.getObject`
stIncMale effeb17
Add internal docs
stIncMale 33d95e3
Do minor improvements
stIncMale 4dbfcaa
Move the `"MongoDB"` literal from `MqlType` to `MongoConstants`
stIncMale d0fd672
Rename `type` -> `code`
stIncMale d11dbcf
Rely on the default `MutabilityPlan`
stIncMale f810311
Separate integration tests
stIncMale 2f15c79
Test `null` as a value of `ObjectId`
stIncMale 60c7345
Take into account that Hibernate ORM may instantiate `ObjectIdJavaTyp…
stIncMale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/integrationTest/java/com/mongodb/hibernate/id/ObjectIdFieldTypeIntegrationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * 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.id; | ||
|
|
||
| import static com.mongodb.hibernate.internal.MongoConstants.ID_FIELD_NAME; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import com.mongodb.client.MongoCollection; | ||
| import com.mongodb.hibernate.junit.InjectMongoCollection; | ||
| import com.mongodb.hibernate.junit.MongoExtension; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import org.bson.BsonDocument; | ||
| import org.bson.BsonObjectId; | ||
| import org.bson.types.ObjectId; | ||
| import org.hibernate.testing.orm.junit.DomainModel; | ||
| import org.hibernate.testing.orm.junit.SessionFactory; | ||
| import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
| import org.hibernate.testing.orm.junit.SessionFactoryScopeAware; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
|
|
||
| @SessionFactory(exportSchema = false) | ||
| @DomainModel(annotatedClasses = {ObjectIdFieldTypeIntegrationTests.Item.class}) | ||
| @ExtendWith(MongoExtension.class) | ||
| class ObjectIdFieldTypeIntegrationTests implements SessionFactoryScopeAware { | ||
| @InjectMongoCollection("items") | ||
| private static MongoCollection<BsonDocument> mongoCollection; | ||
|
|
||
| private SessionFactoryScope sessionFactoryScope; | ||
|
|
||
| @Test | ||
| void insert() { | ||
| var item = new Item(); | ||
| item.id = new ObjectId(1, 0); | ||
| item.v = new ObjectId(2, 0); | ||
| sessionFactoryScope.inTransaction(session -> session.persist(item)); | ||
| assertThat(mongoCollection.find()) | ||
| .containsExactly(new BsonDocument() | ||
| .append(ID_FIELD_NAME, new BsonObjectId(item.id)) | ||
| .append("v", new BsonObjectId(item.v))); | ||
| } | ||
|
|
||
| @Test | ||
| void getById() { | ||
| var item = new Item(); | ||
| item.id = new ObjectId(1, 0); | ||
| item.v = new ObjectId(2, 0); | ||
| sessionFactoryScope.inTransaction(session -> session.persist(item)); | ||
| var loadedItem = sessionFactoryScope.fromTransaction(session -> session.get(Item.class, item.id)); | ||
| assertThat(loadedItem) | ||
| .isNotNull() | ||
| .usingRecursiveComparison() | ||
| .withStrictTypeChecking() | ||
| .isEqualTo(item); | ||
| } | ||
|
|
||
| @Override | ||
| public void injectSessionFactoryScope(SessionFactoryScope sessionFactoryScope) { | ||
| this.sessionFactoryScope = sessionFactoryScope; | ||
| } | ||
|
|
||
| @Entity | ||
| @Table(name = "items") | ||
| static class Item { | ||
| @Id | ||
| ObjectId id; | ||
|
|
||
| ObjectId v; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/main/java/com/mongodb/hibernate/internal/type/MqlType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * 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.type; | ||
|
|
||
| import static com.mongodb.hibernate.internal.MongoAssertions.assertTrue; | ||
|
|
||
| import com.mongodb.hibernate.internal.MongoAssertions; | ||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Modifier; | ||
| import java.sql.SQLType; | ||
| import java.util.Arrays; | ||
| import java.util.function.Predicate; | ||
| import java.util.function.ToIntFunction; | ||
| import org.hibernate.type.SqlTypes; | ||
|
|
||
| public enum MqlType implements SQLType { | ||
| OBJECT_ID(11_000); | ||
|
|
||
| static { | ||
| assertTrue(maxHibernateSqlType() < minType()); | ||
| } | ||
|
|
||
| MqlType(int type) { | ||
| this.type = type; | ||
NathanQingyangXu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private final int type; | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return name(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getVendor() { | ||
| return "MongoDB"; | ||
NathanQingyangXu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public Integer getVendorTypeNumber() { | ||
| return type; | ||
| } | ||
|
|
||
| private static int minType() { | ||
| return Arrays.stream(MqlType.values()) | ||
| .mapToInt(MqlType::getVendorTypeNumber) | ||
| .min() | ||
| .orElseThrow(MongoAssertions::fail); | ||
| } | ||
|
|
||
| private static int maxHibernateSqlType() { | ||
| Predicate<Field> publicStaticFinal = field -> { | ||
| var modifiers = field.getModifiers(); | ||
| return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers); | ||
| }; | ||
| ToIntFunction<Field> valueExtractor = field -> { | ||
| try { | ||
| return field.getInt(null); | ||
| } catch (IllegalAccessException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }; | ||
| return Arrays.stream(SqlTypes.class.getDeclaredFields()) | ||
| .filter(field -> field.getType().equals(int.class)) | ||
| .filter(publicStaticFinal) | ||
| .mapToInt(valueExtractor) | ||
| .max() | ||
| .orElseThrow(MongoAssertions::fail); | ||
| } | ||
NathanQingyangXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
55 changes: 55 additions & 0 deletions
55
src/main/java/com/mongodb/hibernate/internal/type/ObjectIdJavaType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * 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.type; | ||
|
|
||
| import com.mongodb.hibernate.internal.FeatureNotSupportedException; | ||
| import java.io.Serial; | ||
| import org.bson.types.ObjectId; | ||
| import org.hibernate.type.descriptor.WrapperOptions; | ||
| import org.hibernate.type.descriptor.java.AbstractClassJavaType; | ||
| import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan; | ||
| import org.hibernate.type.descriptor.jdbc.JdbcType; | ||
| import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators; | ||
|
|
||
| public final class ObjectIdJavaType extends AbstractClassJavaType<ObjectId> { | ||
| @Serial | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| public static final ObjectIdJavaType INSTANCE = new ObjectIdJavaType(); | ||
|
|
||
| private ObjectIdJavaType() { | ||
| super(ObjectId.class, ImmutableMutabilityPlan.instance()); | ||
NathanQingyangXu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
| public JdbcType getRecommendedJdbcType(JdbcTypeIndicators indicators) { | ||
| return ObjectIdJdbcType.INSTANCE; | ||
| } | ||
|
|
||
| @Override | ||
| public <X> X unwrap(ObjectId value, Class<X> type, WrapperOptions options) { | ||
| throw new FeatureNotSupportedException(); | ||
| } | ||
|
|
||
| @Override | ||
| public <X> ObjectId wrap(X value, WrapperOptions options) { | ||
| if (value instanceof ObjectId wrapped) { | ||
| return wrapped; | ||
| } | ||
| throw new FeatureNotSupportedException(); | ||
NathanQingyangXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
131 changes: 131 additions & 0 deletions
131
src/main/java/com/mongodb/hibernate/internal/type/ObjectIdJdbcType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * 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.type; | ||
|
|
||
| import com.mongodb.hibernate.internal.FeatureNotSupportedException; | ||
| import java.io.Serial; | ||
| import java.sql.CallableStatement; | ||
| import java.sql.PreparedStatement; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.SQLFeatureNotSupportedException; | ||
| import org.bson.types.ObjectId; | ||
| import org.hibernate.type.descriptor.ValueBinder; | ||
| import org.hibernate.type.descriptor.ValueExtractor; | ||
| import org.hibernate.type.descriptor.WrapperOptions; | ||
| import org.hibernate.type.descriptor.java.JavaType; | ||
| import org.hibernate.type.descriptor.jdbc.BasicBinder; | ||
| import org.hibernate.type.descriptor.jdbc.BasicExtractor; | ||
| import org.hibernate.type.descriptor.jdbc.JdbcType; | ||
|
|
||
| public final class ObjectIdJdbcType implements JdbcType { | ||
NathanQingyangXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Serial | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private static final MqlType MQL_TYPE = MqlType.OBJECT_ID; | ||
| private static final ObjectIdJavaType JAVA_TYPE = ObjectIdJavaType.INSTANCE; | ||
| public static final ObjectIdJdbcType INSTANCE = new ObjectIdJdbcType(); | ||
|
|
||
| private ObjectIdJdbcType() {} | ||
|
|
||
| @Override | ||
| public int getJdbcTypeCode() { | ||
| return MQL_TYPE.getVendorTypeNumber(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getFriendlyName() { | ||
| return MQL_TYPE.getName(); | ||
| } | ||
|
|
||
| @Override | ||
| public <X> ValueBinder<X> getBinder(JavaType<X> javaType) { | ||
| if (!javaType.equals(JAVA_TYPE)) { | ||
| throw new FeatureNotSupportedException(); | ||
| } | ||
| @SuppressWarnings("unchecked") | ||
| var result = (ValueBinder<X>) Binder.INSTANCE; | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public <X> ValueExtractor<X> getExtractor(JavaType<X> javaType) { | ||
| if (!javaType.equals(JAVA_TYPE)) { | ||
| throw new FeatureNotSupportedException(); | ||
| } | ||
| @SuppressWarnings("unchecked") | ||
| var result = (ValueExtractor<X>) Extractor.INSTANCE; | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Thread-safe. | ||
| */ | ||
| private static final class Binder extends BasicBinder<ObjectId> { | ||
| @Serial | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| static Binder INSTANCE = new Binder(); | ||
|
|
||
| private Binder() { | ||
| super(JAVA_TYPE, ObjectIdJdbcType.INSTANCE); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doBind(PreparedStatement st, ObjectId value, int index, WrapperOptions options) | ||
| throws SQLException { | ||
| st.setObject(index, value, getJdbcType().getJdbcTypeCode()); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doBind(CallableStatement st, ObjectId value, String name, WrapperOptions options) | ||
| throws SQLException { | ||
| throw new SQLFeatureNotSupportedException(); | ||
| } | ||
NathanQingyangXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Thread-safe. | ||
| */ | ||
| private static final class Extractor extends BasicExtractor<ObjectId> { | ||
| @Serial | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| static Extractor INSTANCE = new Extractor(); | ||
|
|
||
| private Extractor() { | ||
| super(JAVA_TYPE, ObjectIdJdbcType.INSTANCE); | ||
| } | ||
|
|
||
| @Override | ||
| protected ObjectId doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException { | ||
| return rs.getObject(paramIndex, getJavaType().getJavaTypeClass()); | ||
| } | ||
|
|
||
| @Override | ||
| protected ObjectId doExtract(CallableStatement statement, int index, WrapperOptions options) | ||
| throws SQLException { | ||
| throw new SQLFeatureNotSupportedException(); | ||
| } | ||
|
|
||
| @Override | ||
| protected ObjectId doExtract(CallableStatement statement, String name, WrapperOptions options) | ||
| throws SQLException { | ||
| throw new SQLFeatureNotSupportedException(); | ||
| } | ||
| } | ||
| } | ||
NathanQingyangXu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.