-
Notifications
You must be signed in to change notification settings - Fork 11
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
NathanQingyangXu
wants to merge
5
commits into
mongodb:main
Choose a base branch
from
NathanQingyangXu:investigation-HIBERNATE-48
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4cfc163
showcase we can forbid empty struct
NathanQingyangXu 4ea6f3e
showcase @DynamicInsert won't impact Struct
NathanQingyangXu b6021ec
showcase 'compact' insertion and updating (avoid null fields in favou…
NathanQingyangXu 7032c20
showcase empty struct could be fetched intact (not null)
NathanQingyangXu 514fb78
showcase compact struct persistence (leaving out null fields) is poss…
NathanQingyangXu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/integrationTest/java/com/mongodb/hibernate/embeddable/CompactStructIntegrationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2025-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.hibernate.embeddable; | ||
|
||
import static com.mongodb.hibernate.MongoTestAssertions.assertEq; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
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.bson.BsonInt32; | ||
import org.bson.BsonString; | ||
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 = { | ||
CompactStructIntegrationTests.StructHolder.class, | ||
CompactStructIntegrationTests.CompactStruct.class | ||
}) | ||
@ExtendWith(MongoExtension.class) | ||
class CompactStructIntegrationTests { | ||
|
||
@InjectMongoCollection("items") | ||
private static MongoCollection<BsonDocument> mongoCollection; | ||
|
||
@Test | ||
void test(SessionFactoryScope scope) { | ||
var holder = new StructHolder(); | ||
holder.id = 1; | ||
holder.compactStruct = new CompactStruct(); | ||
holder.compactStruct.field1 = null; | ||
holder.compactStruct.field2 = "value2"; | ||
scope.inTransaction(session -> session.persist(holder)); | ||
|
||
assertThat(mongoCollection.find()) | ||
.containsExactly(new BsonDocument("_id", new BsonInt32(1)) | ||
.append("compactStruct", new BsonDocument("field2", new BsonString("value2")))); | ||
|
||
var loadedHolder = scope.fromTransaction(session -> session.find(StructHolder.class, 1)); | ||
assertEq(holder, loadedHolder); | ||
} | ||
|
||
@Entity(name = "StructHolder") | ||
@Table(name = "items") | ||
static class StructHolder { | ||
@Id | ||
int id; | ||
|
||
CompactStruct compactStruct; | ||
} | ||
|
||
@Embeddable | ||
@Struct(name = "CompactStruct") | ||
static class CompactStruct { | ||
String field1; | ||
String field2; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...m/mongodb/hibernate/embeddable/DynamicInsertWithStructWithNullValuesIntegrationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2025-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.hibernate.embeddable; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
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; | ||
|
||
@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; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
.../java/com/mongodb/hibernate/embeddable/EmptyStructAggregateRetrievalIntegrationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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.ServiceRegistry; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.hibernate.testing.orm.junit.Setting; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import static com.mongodb.hibernate.MongoTestAssertions.assertEq; | ||
|
||
@SessionFactory(exportSchema = false) | ||
@DomainModel(annotatedClasses = { | ||
EmptyStructAggregateRetrievalIntegrationTests.Book.class, | ||
EmptyStructAggregateRetrievalIntegrationTests.Author.class | ||
}) | ||
@ExtendWith(MongoExtension.class) | ||
class EmptyStructAggregateRetrievalIntegrationTests { | ||
|
||
@Test | ||
void testEmptyStructAggregateRetriedAsNonNull(SessionFactoryScope scope) { | ||
var book = new Book(); | ||
book.id = 1; | ||
book.author = new Author(); | ||
|
||
scope.inTransaction(session -> session.persist(book)); | ||
|
||
var retrievedBook = scope.fromTransaction(session -> session.get(Book.class, 1)); | ||
assertEq(book, retrievedBook); | ||
} | ||
|
||
@Entity | ||
@Table(name = "books") | ||
static class Book { | ||
@Id int id; | ||
Author author; | ||
} | ||
|
||
@Embeddable | ||
@Struct(name = "Author") | ||
static class Author { | ||
String firstName; | ||
String lastName; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
My understanding is that this test requires
hibernate.create_empty_composites.enabled=true
. If that's true, then it does not "showcase empty struct could be fetched intact (not null)", as we won't use this configuration property knowing it does not exist in Hibernate ORM 7.0.We should stop bringing up
hibernate.create_empty_composites.enabled
to avoid confusion.