Skip to content

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 12 commits into from
Apr 1, 2025
Merged

Add support for ObjectId #70

merged 12 commits into from
Apr 1, 2025

Conversation

stIncMale
Copy link
Member

@stIncMale stIncMale self-assigned this Mar 17, 2025
Comment on lines +135 to +149
var expectedDoc = new BsonDocument()
.append("insert", new BsonString("items"))
.append(
"documents",
new BsonArray(List.of(new BsonDocument()
.append("string1", new BsonString("s1"))
.append("string2", new BsonString("s2"))
.append("int32", new BsonInt32(1))
.append("boolean", BsonBoolean.TRUE)
.append(
"stringAndObjectId",
new BsonArray(List.of(
new BsonString("array element"),
new BsonObjectId(new ObjectId(1, 2)))))
.append("objectId", new BsonObjectId(new ObjectId(2, 0))))));
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 way it is easier to express what exactly we are expecting than with a string.

@stIncMale stIncMale marked this pull request as ready for review March 27, 2025 05:10
@NathanQingyangXu
Copy link
Contributor

Looks great overall. Only some minor comments are left.

@NathanQingyangXu
Copy link
Contributor

Just found an edge case wherein explicit @JavaType annotation is used, the getById() testing case would fail:

@Entity
    @Table(name = "items")
    static class Item {
        @Id
        ObjectId id;

        @JavaType(ObjectIdJavaType.class)
        ObjectId v;
    }

@stIncMale
Copy link
Member Author

Regarding #70 (comment)

Just found an edge case wherein explicit @JavaType annotation is used, the getById() testing case would fail

This is because Hibernate ORM instantiates ObjectIdJavaType on its own in that case. Consequently, when it calls ObjectIdJdbcType.getBinder, the instance of ObjectIdJavaType passed as the javaType argument is not the same as ObjectIdJavaType.INSTANCE. I added the ObjectIdJavaType.equals/hashCode overrides to take this into account. However, applications can't explicitly use ObjectIdJavaType, ObjectIdJdbcType, because they are both internal. Are you aware of any reason to make them part of the API now?

Done in 60c7345.

@NathanQingyangXu
Copy link
Contributor

Regarding #70 (comment)

Just found an edge case wherein explicit @JavaType annotation is used, the getById() testing case would fail

This is because Hibernate ORM instantiates ObjectIdJavaType on its own in that case. Consequently, when it calls ObjectIdJdbcType.getBinder, the instance of ObjectIdJavaType passed as the javaType argument is not the same as ObjectIdJavaType.INSTANCE. I added the ObjectIdJavaType.equals/hashCode overrides to take this into account. However, applications can't explicitly use ObjectIdJavaType, ObjectIdJdbcType, because they are both internal. Are you aware of any reason to make them part of the API now?

Done in 60c7345.

If they are internal, I think the edge case doesn't make sense, right? I missed that point. Should we revert back this commit then if it is not a concern any more?

Copy link
Contributor

@NathanQingyangXu NathanQingyangXu left a comment

Choose a reason for hiding this comment

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

Let us revert back the edge-case related stuff if these type classes are internal ones, not meant to be used externally

@NathanQingyangXu
Copy link
Contributor

Let us revert back the edge-case related stuff if these type classes are internal ones, not meant to be used externally

It doesn't hurt to leave them as they are. It might improve our product robustness.

@stIncMale
Copy link
Member Author

Let us revert back the edge-case related stuff

Talked about this, and agreed to leave the changes: despite applications not being able to use @JavaType(ObjectIdJavaType.class), this example gave us knowledge that Hibernate ORM may instantiate an implementation of JavaType on its own, even if it is not designed to be externally instantiated (for example, has a private constructor; note how Hibernate ORM does not do the same for entities, so this behavior is weird to me). Taking this behavior into account in our code seems better, even if we don't expect it. The alternative is to throw FeatureNotSupportedException, and leave an comment explaining what is going on. Having a test that gives an example seems better than having a comment.

@stIncMale stIncMale merged commit 61343c0 into mongodb:main Apr 1, 2025
6 checks passed
@stIncMale stIncMale deleted the HIBERNATE-23 branch April 1, 2025 14:19
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