Skip to content
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

Add support for ObjectId #70

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

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.


@Override
public String getVendor() {
return "MongoDB";
Copy link
Contributor

Choose a reason for hiding this comment

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

optional. move it to MongoConstants?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 4dbfcaa. The rest of the relevant changes is in #79.

}

MqlType(int type) {
this.type = type;
Copy link
Contributor

Choose a reason for hiding this comment

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

from my understanding, code or typeCode is more accurate. SQLType or MqlType is a wrapper of the code or number, but the type per se is not equal to code or number.

We might need to rename the two static methods as well:

  • maxHibernateSqlType -> maxHibernateSqlTypeCode
  • minType -> minMqlTypeCode

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree. I used type because that's how java.sql.JDBCType names it (see JDBCType.type), and java.sql.JDBCType is the closest thing to MqlType. The JDBC naming of this concept is inconsistent:

  1. The non-API part of the JDBC specification calls it "type code".
  2. java.sql.Types documents each constant it defines as "type code", yet the class itself is not called TypeCodes, but Types.
  3. Seemingly, as a result of the previous item, java.sql.JDBCType calls its code field JDBCType.type.
  4. java.sql.SQLType documents the same thing as "vendor specific type number", and exposes the method SQLType.getVendorTypeNumber.

Done in d0fd672.

@Id
ObjectId id;

ObjectId v;
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be great to add testing case covering such edge case: persisting non-id field with null value.

It might be better orgnaized to create two separate testing case suites, one of which focuses on id and the other focuses on non-id field.

import org.hibernate.type.descriptor.jdbc.BasicExtractor;
import org.hibernate.type.descriptor.jdbc.JdbcType;

public final class ObjectIdJdbcType implements JdbcType {
Copy link
Contributor

Choose a reason for hiding this comment

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

optional. It would be great to implement toString() if only for better debugging purpose.

@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;
    }

public static final ObjectIdJavaType INSTANCE = new ObjectIdJavaType();

private ObjectIdJavaType() {
super(ObjectId.class, ImmutableMutabilityPlan.instance());
Copy link
Contributor

Choose a reason for hiding this comment

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

it doesn't hurt to specify ImmutableMutabilityPlan above, but given it is default plan, super(ObjectId.class) seems to suffice.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in d11dbcf.

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