-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
}); | ||
} | ||
|
||
@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 |
---|---|---|
|
@@ -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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this work for empty There was a problem hiding this comment. Choose a reason for hiding this commentThe 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).
|
||
throw new FeatureNotSupportedException(format("empty struct: %s, are you kidding me?", component.getComponentClass().getName())); | ||
} | ||
}); | ||
} | ||
|
||
private static void checkColumnNames(PersistentClass persistentClass) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).