Skip to content

Commit 21de6ea

Browse files
authored
Merge pull request #272 from eclipse/feat/mongodb-multi-level-persistence-issue
Fixed the persistence of entities with a list of sub-entities at JNoSQL MongoDB API
2 parents 557faed + dc5b1fe commit 21de6ea

File tree

7 files changed

+118
-2
lines changed

7 files changed

+118
-2
lines changed

CHANGELOG.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
88

99
== [Unreleased]
1010

11+
1112
=== Changed
1213

1314
- Update ArangoDB driver to 7.5.1
@@ -25,6 +26,7 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
2526
=== Fixed
2627

2728
- Allow multiple entities at Oracle NoSQL appending the entity name with the id instead of only the id
29+
- Allow storing of entities with list of sub-entities at MongoDB
2830

2931
== [1.1.0] - 2023-02-05
3032

jnosql-mongodb/src/main/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private static Object getMap(Object val) {
106106
Map<Object, Object> map = new HashMap<>();
107107
for (Object item : iterable) {
108108
var document = cast(item);
109-
map.put(document.name(), document.get());
109+
map.put(document.name(), convert(document.value()));
110110
}
111111
return map;
112112
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2023 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Maximillian Arruda
14+
*/
15+
16+
package org.eclipse.jnosql.databases.mongodb.integration;
17+
18+
import jakarta.nosql.Column;
19+
import jakarta.nosql.Entity;
20+
import jakarta.nosql.Id;
21+
22+
import java.util.List;
23+
24+
@Entity
25+
public record BookOrder(@Id String id, @Column List<BookOrderItem> items){
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2023 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Maximillian Arruda
14+
*/
15+
package org.eclipse.jnosql.databases.mongodb.integration;
16+
17+
import jakarta.nosql.Column;
18+
import jakarta.nosql.Entity;
19+
20+
@Entity
21+
public record BookOrderItem(@Column Book book, @Column Integer quantity) {
22+
}

jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/BookRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*
1313
* Maximillian Arruda
1414
*/
15+
1516
package org.eclipse.jnosql.databases.mongodb.integration;
1617

1718
import jakarta.data.repository.Repository;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2023 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Maximillian Arruda
14+
*/
15+
16+
package org.eclipse.jnosql.databases.mongodb.integration;
17+
18+
import jakarta.data.repository.Repository;
19+
import org.eclipse.jnosql.mapping.NoSQLRepository;
20+
21+
@Repository
22+
public interface BookStore extends NoSQLRepository<BookOrder, String> {
23+
}

jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/integration/RepositoryIntegrationTest.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.eclipse.jnosql.databases.mongodb.integration;
1616

1717
import jakarta.inject.Inject;
18+
import org.assertj.core.api.SoftAssertions;
1819
import org.eclipse.jnosql.databases.mongodb.communication.MongoDBDocumentConfigurations;
1920
import org.eclipse.jnosql.databases.mongodb.mapping.MongoDBTemplate;
2021
import jakarta.nosql.Convert;
@@ -33,8 +34,12 @@
3334
import org.junit.jupiter.api.Test;
3435
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
3536

37+
import java.util.List;
38+
3639
import static java.util.UUID.randomUUID;
40+
import static org.assertj.core.api.Assertions.as;
3741
import static org.assertj.core.api.Assertions.assertThat;
42+
import static org.assertj.core.api.SoftAssertions.assertSoftly;
3843
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
3944
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;
4045
import static org.eclipse.jnosql.databases.mongodb.communication.DocumentDatabase.INSTANCE;
@@ -59,6 +64,11 @@ class RepositoryIntegrationTest {
5964
@Database(DatabaseType.DOCUMENT)
6065
BookRepository repository;
6166

67+
68+
@Inject
69+
@Database(DatabaseType.DOCUMENT)
70+
BookStore bookStore;
71+
6272
@Test
6373
void shouldSave() {
6474
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
@@ -75,6 +85,36 @@ void shouldSave() {
7585
assertThat(repository.findById(book.id()))
7686
.isNotNull().get().isEqualTo(updated);
7787

88+
assertSoftly(softly -> {
89+
90+
BookOrder order = new BookOrder(
91+
randomUUID().toString(),
92+
List.of(
93+
new BookOrderItem(new Book(randomUUID().toString(), "Effective Java", 3), 1)
94+
,new BookOrderItem(new Book(randomUUID().toString(), "Java Persistence Layer", 1), 10)
95+
,new BookOrderItem(new Book(randomUUID().toString(), "Jakarta EE Cookbook", 1), 5)
96+
)
97+
);
98+
99+
bookStore.save(order);
100+
101+
softly.assertThat(bookStore.findById(order.id()))
102+
.as("cannot find the order persisted previously")
103+
.isPresent()
104+
.get()
105+
.as("the loaded the persisted BookOrder doesn't matches with the BookOrder origin")
106+
.satisfies(persistedOrder ->{
107+
softly.assertThat(persistedOrder.id())
108+
.as("the loaded the persisted BookOrder id is not equals to the BookOrder origin id")
109+
.isEqualTo(order.id());
110+
softly.assertThat(persistedOrder.items())
111+
.as("the loaded the persisted BookOrder items is not equals to the BookOrder origin items")
112+
.containsAll(order.items());
113+
});
114+
115+
116+
});
117+
78118
}
79119

80120
@Test
@@ -111,7 +151,9 @@ void shouldDeleteAll() {
111151
}
112152

113153
repository.deleteAll();
114-
assertThat(repository.findAll()).isEmpty();
154+
bookStore.deleteAll();
155+
assertThat(repository.findAll()).as("the repository is not empty").isEmpty();
156+
assertThat(bookStore.findAll()).as("the bookStore is not empty").isEmpty();
115157
}
116158

117159
}

0 commit comments

Comments
 (0)