-
Notifications
You must be signed in to change notification settings - Fork 11
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 all 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
77 changes: 77 additions & 0 deletions
77
src/integrationTest/java/com/mongodb/hibernate/id/ObjectIdAsIdIntegrationTests.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,77 @@ | ||
/* | ||
* 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 static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
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 = {ObjectIdAsIdIntegrationTests.Item.class}) | ||
@ExtendWith(MongoExtension.class) | ||
class ObjectIdAsIdIntegrationTests 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); | ||
sessionFactoryScope.inTransaction(session -> session.persist(item)); | ||
assertThat(mongoCollection.find()) | ||
.containsExactly(new BsonDocument().append(ID_FIELD_NAME, new BsonObjectId(item.id))); | ||
} | ||
|
||
@Test | ||
void getById() { | ||
var item = new Item(); | ||
item.id = new ObjectId(1, 0); | ||
sessionFactoryScope.inTransaction(session -> session.persist(item)); | ||
var loadedItem = sessionFactoryScope.fromTransaction(session -> session.get(Item.class, item.id)); | ||
assertEquals(item.id, loadedItem.id); | ||
} | ||
|
||
@Override | ||
public void injectSessionFactoryScope(SessionFactoryScope sessionFactoryScope) { | ||
this.sessionFactoryScope = sessionFactoryScope; | ||
} | ||
|
||
@Entity | ||
@Table(name = "items") | ||
static class Item { | ||
@Id | ||
ObjectId id; | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
src/integrationTest/java/com/mongodb/hibernate/type/ObjectIdIntegrationTests.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,117 @@ | ||
/* | ||
* 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.type; | ||
|
||
import static com.mongodb.hibernate.internal.MongoConstants.ID_FIELD_NAME; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.hibernate.internal.type.ObjectIdJavaType; | ||
import com.mongodb.hibernate.internal.type.ObjectIdJdbcType; | ||
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 java.util.Objects; | ||
import org.bson.BsonDocument; | ||
import org.bson.BsonInt32; | ||
import org.bson.BsonNull; | ||
import org.bson.BsonObjectId; | ||
import org.bson.types.ObjectId; | ||
import org.hibernate.annotations.JavaType; | ||
import org.hibernate.annotations.JdbcType; | ||
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 = {ObjectIdIntegrationTests.Item.class}) | ||
@ExtendWith(MongoExtension.class) | ||
class ObjectIdIntegrationTests implements SessionFactoryScopeAware { | ||
@InjectMongoCollection("items") | ||
private static MongoCollection<BsonDocument> mongoCollection; | ||
|
||
private SessionFactoryScope sessionFactoryScope; | ||
|
||
@Test | ||
void insert() { | ||
var item = new Item(); | ||
item.id = 1; | ||
item.v = new ObjectId(1, 0); | ||
item.vExplicitlyAnnotatedNotForThePublic = new ObjectId(2, 3); | ||
sessionFactoryScope.inTransaction(session -> session.persist(item)); | ||
assertThat(mongoCollection.find()) | ||
.containsExactly(new BsonDocument() | ||
.append(ID_FIELD_NAME, new BsonInt32(1)) | ||
.append("v", new BsonObjectId(item.v)) | ||
.append("vNull", BsonNull.VALUE) | ||
.append( | ||
"vExplicitlyAnnotatedNotForThePublic", | ||
new BsonObjectId(item.vExplicitlyAnnotatedNotForThePublic))); | ||
} | ||
|
||
@Test | ||
void getById() { | ||
var item = new Item(); | ||
item.id = 1; | ||
item.v = new ObjectId(2, 0); | ||
sessionFactoryScope.inTransaction(session -> session.persist(item)); | ||
var loadedItem = sessionFactoryScope.fromTransaction(session -> session.get(Item.class, item.id)); | ||
assertEquals(item, loadedItem); | ||
} | ||
|
||
@Override | ||
public void injectSessionFactoryScope(SessionFactoryScope sessionFactoryScope) { | ||
this.sessionFactoryScope = sessionFactoryScope; | ||
} | ||
|
||
@Entity | ||
@Table(name = "items") | ||
static class Item { | ||
@Id | ||
int id; | ||
|
||
ObjectId v; | ||
ObjectId vNull; | ||
|
||
@JavaType(ObjectIdJavaType.class) | ||
@JdbcType(ObjectIdJdbcType.class) | ||
ObjectId vExplicitlyAnnotatedNotForThePublic; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
Item item = (Item) o; | ||
return id == item.id | ||
&& Objects.equals(v, item.v) | ||
&& Objects.equals(vNull, item.vNull) | ||
&& Objects.equals(vExplicitlyAnnotatedNotForThePublic, item.vExplicitlyAnnotatedNotForThePublic); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, v, vNull, vExplicitlyAnnotatedNotForThePublic); | ||
} | ||
} | ||
} |
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
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
85 changes: 85 additions & 0 deletions
85
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,85 @@ | ||
/* | ||
* 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 static com.mongodb.hibernate.internal.MongoConstants.MONGO_DBMS_NAME; | ||
|
||
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(maxHibernateSqlTypeCode() < minMqlTypeCode()); | ||
} | ||
|
||
MqlType(int code) { | ||
this.code = code; | ||
} | ||
|
||
private final int code; | ||
|
||
@Override | ||
public String getName() { | ||
return name(); | ||
} | ||
|
||
@Override | ||
public String getVendor() { | ||
return MONGO_DBMS_NAME; | ||
} | ||
|
||
@Override | ||
public Integer getVendorTypeNumber() { | ||
return code; | ||
} | ||
|
||
private static int minMqlTypeCode() { | ||
return Arrays.stream(MqlType.values()) | ||
.mapToInt(MqlType::getVendorTypeNumber) | ||
.min() | ||
.orElseThrow(MongoAssertions::fail); | ||
} | ||
|
||
private static int maxHibernateSqlTypeCode() { | ||
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); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
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,67 @@ | ||
/* | ||
* 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.util.concurrent.ThreadLocalRandom; | ||
import org.bson.types.ObjectId; | ||
import org.hibernate.type.descriptor.WrapperOptions; | ||
import org.hibernate.type.descriptor.java.AbstractClassJavaType; | ||
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; | ||
|
||
private static final int hashCode = ThreadLocalRandom.current().nextInt(); | ||
|
||
public static final ObjectIdJavaType INSTANCE = new ObjectIdJavaType(); | ||
|
||
private ObjectIdJavaType() { | ||
super(ObjectId.class); | ||
} | ||
|
||
@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
|
||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
return o != null && getClass() == o.getClass(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return hashCode; | ||
} | ||
} |
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.