Skip to content

Hibernate-48 investigation #105

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.mongodb.hibernate.embeddable;

import com.mongodb.client.MongoCollection;
import com.mongodb.hibernate.junit.InjectMongoCollection;
import com.mongodb.hibernate.junit.MongoExtension;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.bson.BsonDocument;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.Struct;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.assertj.core.api.Assertions.assertThat;

@SessionFactory(exportSchema = false)
@DomainModel(
annotatedClasses = {
DynamicInsertWithStructWithNullValuesIntegrationTests.Book.class,
DynamicInsertWithStructWithNullValuesIntegrationTests.Author.class
})
@ExtendWith(MongoExtension.class)
class DynamicInsertWithStructWithNullValuesIntegrationTests {

@InjectMongoCollection("books")
private static MongoCollection<BsonDocument> mongoCollection;

@Test
void test(SessionFactoryScope scope) {
scope.inTransaction(session -> {
var book = new Book();
book.id = 1;
book.author = new Author();
session.persist(book);
});
assertThat(mongoCollection.find()).containsExactly(
BsonDocument.parse(
"""
{
_id: 1,
author: {
firstName: null,
lastName: null
}
}
""")
);
}


@Entity
@DynamicInsert
@Table(name = "books")
static class Book {
@Id
int id;
Author author;
}

@Embeddable
@Struct(name = "Author")
static class Author {
String firstName;
String lastName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.mongodb.hibernate.embeddable;

import com.mongodb.hibernate.junit.MongoExtension;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.annotations.Struct;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@SessionFactory(exportSchema = false)
@DomainModel(
annotatedClasses = {
EmptyStructIntegrationTests.StructHolder.class,
EmptyStructIntegrationTests.EmptyStruct.class
})
@ExtendWith(MongoExtension.class)
class EmptyStructIntegrationTests {


@Test
void test(SessionFactoryScope scope) {
scope.inTransaction(session -> {
var holder = new StructHolder();
holder.id = 1;
holder.emptyStruct = new EmptyStruct();
session.persist(holder);
Copy link
Member

@vbabanin vbabanin Jun 12, 2025

Choose a reason for hiding this comment

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

What is the produced MQL result in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

{"insert": "books", "documents": [{"author": {"firstName": null, "lastName": null}, "_id": 1}]}

From my understanding, @DynamicInsert only covers an entity's root level fields. If the field is special type like STRUCT or array, it goes out of its scope how they are gonna to be persisted, as long as the field per se is non-null.

In our product, we use STRUCT to implement embedded doc and seeminly the same behavour should apply to the embeded doc; but this is our wishful thinking, for STRUCT is mainly used for SQL's User Defined Type. In traditional Hibernate design, all fields value should be provided to STRUCT (there is no analogy of @DynamicInsert for STRUCT; again, @DynamicInsert is only meant for entity class).

});
}

@Entity
@Table(name = "collection")
static class StructHolder {
@Id
int id;
EmptyStruct emptyStruct;
}

@Embeddable
@Struct(name = "EmptyStruct")
static class EmptyStruct {
// No fields
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,15 @@ public void contribute(
ResourceStreamLocator resourceStreamLocator,
MetadataBuildingContext buildingContext) {
metadata.getEntityBindings().forEach(persistentClass -> {
forbidDynamicInsert(persistentClass);
checkColumnNames(persistentClass);
forbidStructIdentifier(persistentClass);
setIdentifierColumnName(persistentClass);
});
}

private static void forbidDynamicInsert(PersistentClass persistentClass) {
if (persistentClass.useDynamicInsert()) {
throw new FeatureNotSupportedException(format("%s is not supported", DynamicInsert.class.getSimpleName()));
}
metadata.visitRegisteredComponents(component -> {
if (component.getStructName() != null && component.getProperties().isEmpty()) {
Copy link
Member

Choose a reason for hiding this comment

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

Does this work for empty @Embeddables that are not @Struct? Hibernate ORM also omits storing them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, the above only applies to STRUCT (STRUCT must have name so telling whether the struct name is present suffices).
If we want to enlarge the forbidding to any @Embeddable, I think we simply drop the struct name checking above as below:

if (component.getProperties().isEmpty()) {
    ... ...
}

throw new FeatureNotSupportedException(format("empty struct: %s, are you kidding me?", component.getComponentClass().getName()));
}
});
}

private static void checkColumnNames(PersistentClass persistentClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ public BsonDocument createJdbcValue(Object domainValue, WrapperOptions options)
}
var fieldName = jdbcValueSelectable.getSelectableName();
var value = embeddableMappingType.getValue(domainValue, columnIndex);
if (value == null) {
throw new FeatureNotSupportedException(
"TODO-HIBERNATE-48 https://jira.mongodb.org/browse/HIBERNATE-48");
}
BsonValue bsonValue;
var jdbcMapping = jdbcValueSelectable.getJdbcMapping();
if (jdbcMapping.getJdbcType().getJdbcTypeCode() == JDBC_TYPE.getVendorTypeNumber()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import org.bson.BsonDouble;
import org.bson.BsonInt32;
import org.bson.BsonInt64;
import org.bson.BsonNull;
import org.bson.BsonObjectId;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.types.Decimal128;
import org.bson.types.ObjectId;
import org.jspecify.annotations.Nullable;

/**
* Provides conversion methods between {@link BsonValue}s, which our {@link PreparedStatement}/{@link ResultSet}
Expand All @@ -43,8 +45,10 @@
public final class ValueConversions {
private ValueConversions() {}

public static BsonValue toBsonValue(Object value) throws SQLFeatureNotSupportedException {
assertNotNull(value);
public static BsonValue toBsonValue(@Nullable Object value) throws SQLFeatureNotSupportedException {
if (value == null) {
return BsonNull.VALUE;
}
if (value instanceof Boolean v) {
return toBsonValue(v.booleanValue());
} else if (value instanceof Integer v) {
Expand Down