Skip to content

Commit bff3df1

Browse files
committed
Add ObjectIdFieldTypeIntegrationTests.getById
1 parent 8ac53b2 commit bff3df1

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

src/integrationTest/java/com/mongodb/hibernate/id/ObjectIdFieldTypeIntegrationTests.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,28 @@ class ObjectIdFieldTypeIntegrationTests implements SessionFactoryScopeAware {
4646

4747
@Test
4848
void insert() {
49-
var id = new ObjectId(1, 0);
50-
sessionFactoryScope.inTransaction(session -> {
51-
var item = new Item();
52-
item.id = id;
53-
session.persist(item);
54-
});
55-
assertThat(mongoCollection.find()).containsExactly(new BsonDocument(ID_FIELD_NAME, new BsonObjectId(id)));
49+
var item = new Item();
50+
item.id = new ObjectId(1, 0);
51+
item.v = new ObjectId(2, 0);
52+
sessionFactoryScope.inTransaction(session -> session.persist(item));
53+
assertThat(mongoCollection.find())
54+
.containsExactly(new BsonDocument(ID_FIELD_NAME, new BsonObjectId(item.id))
55+
.append("v", new BsonObjectId(item.v)));
5656
}
5757

58-
// VAKOTODO add read tests
58+
@Test
59+
void getById() {
60+
var item = new Item();
61+
item.id = new ObjectId(1, 0);
62+
item.v = new ObjectId(2, 0);
63+
sessionFactoryScope.inTransaction(session -> session.persist(item));
64+
var loadedItem = sessionFactoryScope.fromTransaction(session -> session.get(Item.class, item.id));
65+
assertThat(loadedItem)
66+
.isNotNull()
67+
.usingRecursiveComparison()
68+
.withStrictTypeChecking()
69+
.isEqualTo(item);
70+
}
5971

6072
@Override
6173
public void injectSessionFactoryScope(SessionFactoryScope sessionFactoryScope) {
@@ -67,5 +79,7 @@ public void injectSessionFactoryScope(SessionFactoryScope sessionFactoryScope) {
6779
static class Item {
6880
@Id
6981
ObjectId id;
82+
83+
ObjectId v;
7084
}
7185
}

src/main/java/com/mongodb/hibernate/internal/type/ObjectIdJavaType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public <X> X unwrap(ObjectId value, Class<X> type, WrapperOptions options) {
4747

4848
@Override
4949
public <X> ObjectId wrap(X value, WrapperOptions options) {
50+
if (value instanceof ObjectId wrapped) {
51+
return wrapped;
52+
}
5053
throw new FeatureNotSupportedException();
5154
}
5255
}

src/main/java/com/mongodb/hibernate/jdbc/MongoResultSet.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.util.function.Function;
5252
import org.bson.BsonDocument;
5353
import org.bson.BsonValue;
54+
import org.bson.types.ObjectId;
5455
import org.jspecify.annotations.Nullable;
5556

5657
final class MongoResultSet implements ResultSetAdapter {
@@ -202,10 +203,16 @@ public double getDouble(int columnIndex) throws SQLException {
202203

203204
@Override
204205
public <T> @Nullable T getObject(int columnIndex, Class<T> type) throws SQLException {
205-
// VAKOTODO implement reading and add MongoResultSet tests
206+
// VAKOTODO add MongoResultSet tests
206207
checkClosed();
207208
checkColumnIndex(columnIndex);
208-
throw new SQLFeatureNotSupportedException("To be implemented in scope of Array / Struct tickets");
209+
Object value;
210+
if (type.equals(ObjectId.class)) {
211+
value = getValue(columnIndex, bsonValue -> bsonValue.asObjectId().getValue());
212+
} else {
213+
throw new SQLFeatureNotSupportedException("To be implemented in scope of Array / Struct tickets");
214+
}
215+
return type.cast(value);
209216
}
210217

211218
@Override

0 commit comments

Comments
 (0)