Skip to content

Commit 5ab75eb

Browse files
committed
Polishing.
Reduce dependencies in tests by using NoOpDbRefResolver. Add since tags. Tweak documentation. Extract entity references into own documentation fragment. Original pull request: #3647. Closes #3602.
1 parent e96ef8e commit 5ab75eb

23 files changed

+680
-654
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

-2
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,6 @@ private void readAssociation(Association<MongoPersistentProperty> association, P
546546

547547
DBRef dbref = value instanceof DBRef ? (DBRef) value : null;
548548

549-
// TODO: accessor.setProperty(property, dbRefResolver.resolveReference(property, value, referenceReader,
550-
// context::convert));
551549
accessor.setProperty(property, dbRefResolver.resolveDbRef(property, dbref, callback, handler));
552550
}
553551

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MongoDatabaseFactoryReferenceLoader.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
/**
3030
* {@link ReferenceLoader} implementation using a {@link MongoDatabaseFactory} to obtain raw {@link Document documents}
3131
* for linked entities via a {@link ReferenceLoader.DocumentReferenceQuery}.
32-
*
32+
*
3333
* @author Christoph Strobl
34+
* @since 3.3
3435
*/
3536
public class MongoDatabaseFactoryReferenceLoader implements ReferenceLoader {
3637

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceLoader.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
/**
2929
* The {@link ReferenceLoader} obtains raw {@link Document documents} for linked entities via a
3030
* {@link ReferenceLoader.DocumentReferenceQuery}.
31-
*
31+
*
3232
* @author Christoph Strobl
3333
* @since 3.3
3434
*/
@@ -79,7 +79,6 @@ default Bson getSort() {
7979
return new Document();
8080
}
8181

82-
// TODO: Move apply method into something else that holds the collection and knows about single item/multi-item
8382
default Iterable<Document> apply(MongoCollection<Document> collection) {
8483
return restoreOrder(collection.find(getQuery()).sort(getSort()));
8584
}

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceLookupDelegate.java

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
*
5858
* @author Christoph Strobl
5959
* @author Mark Paluch
60+
* @since 3.3
6061
*/
6162
public final class ReferenceLookupDelegate {
6263

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/DocumentPointer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
/**
1919
* A custom pointer to a linked document to be used along with {@link DocumentReference} for storing the linkage value.
20-
*
20+
*
2121
* @author Christoph Strobl
22+
* @since 3.3
2223
*/
2324
@FunctionalInterface
2425
public interface DocumentPointer<T> {
@@ -27,7 +28,7 @@ public interface DocumentPointer<T> {
2728
* The actual pointer value. This can be any simple type, like a {@link String} or {@link org.bson.types.ObjectId} or
2829
* a {@link org.bson.Document} holding more information like the target collection, multiple fields forming the key,
2930
* etc.
30-
*
31+
*
3132
* @return the value stored in MongoDB and used for constructing the {@link DocumentReference#lookup() lookup query}.
3233
*/
3334
T getPointer();

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/DocumentReference.java

+32-32
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
import java.lang.annotation.Target;
2323

2424
import org.springframework.data.annotation.Reference;
25+
import org.springframework.data.mongodb.MongoDatabaseFactory;
2526

2627
/**
27-
* A {@link DocumentReference} offers an alternative way of linking entities in MongoDB. While the goal is the same as
28-
* when using {@link DBRef}, the store representation is different and can be literally anything, a single value, an
29-
* entire {@link org.bson.Document}, basically everything that can be stored in MongoDB. By default, the mapping layer
30-
* will use the referenced entities {@literal id} value for storage and retrieval.
31-
*
28+
* A {@link DocumentReference} allows referencing entities in MongoDB using a flexible schema. While the goal is the
29+
* same as when using {@link DBRef}, the store representation is different. The reference can be anything, a single
30+
* value, an entire {@link org.bson.Document}, basically everything that can be stored in MongoDB. By default, the
31+
* mapping layer will use the referenced entities {@literal id} value for storage and retrieval.
32+
*
3233
* <pre class="code">
3334
* public class Account {
3435
* private String id;
@@ -40,7 +41,7 @@
4041
* &#64;DocumentReference
4142
* private List&lt;Account&gt; accounts;
4243
* }
43-
*
44+
*
4445
* Account account = ...
4546
*
4647
* mongoTemplate.insert(account);
@@ -50,43 +51,41 @@
5051
* .apply(new Update().push("accounts").value(account))
5152
* .first();
5253
* </pre>
53-
*
54-
* {@link #lookup()} allows to define custom queries that are independent from the {@literal id} field and in
55-
* combination with {@link org.springframework.data.convert.WritingConverter writing converters} offer a flexible way of
56-
* defining links between entities.
57-
*
54+
*
55+
* {@link #lookup()} allows defining a query filter that is independent from the {@literal _id} field and in combination
56+
* with {@link org.springframework.data.convert.WritingConverter writing converters} offers a flexible way of defining
57+
* references between entities.
58+
*
5859
* <pre class="code">
5960
* public class Book {
60-
* private ObjectId id;
61-
* private String title;
61+
* private ObjectId id;
62+
* private String title;
6263
*
63-
* &#64;Field("publisher_ac")
64-
* &#64;DocumentReference(lookup = "{ 'acronym' : ?#{#target} }")
65-
* private Publisher publisher;
64+
* &#64;Field("publisher_ac") &#64;DocumentReference(lookup = "{ 'acronym' : ?#{#target} }") private Publisher publisher;
6665
* }
6766
*
6867
* public class Publisher {
6968
*
70-
* private ObjectId id;
71-
* private String acronym;
72-
* private String name;
69+
* private ObjectId id;
70+
* private String acronym;
71+
* private String name;
7372
*
74-
* &#64;DocumentReference(lazy = true)
75-
* private List&lt;Book&gt; books;
73+
* &#64;DocumentReference(lazy = true) private List&lt;Book&gt; books;
7674
* }
7775
*
7876
* &#64;WritingConverter
7977
* public class PublisherReferenceConverter implements Converter&lt;Publisher, DocumentPointer&lt;String&gt;&gt; {
8078
*
81-
* public DocumentPointer&lt;String&gt; convert(Publisher source) {
79+
* public DocumentPointer&lt;String&gt; convert(Publisher source) {
8280
* return () -> source.getAcronym();
83-
* }
81+
* }
8482
* }
8583
* </pre>
8684
*
8785
* @author Christoph Strobl
8886
* @since 3.3
89-
* @see <a href="https://docs.mongodb.com/manual/reference/database-references/#std-label-document-references">MongoDB Reference Documentation</a>
87+
* @see <a href="https://docs.mongodb.com/manual/reference/database-references/#std-label-document-references">MongoDB
88+
* Reference Documentation</a>
9089
*/
9190
@Documented
9291
@Retention(RetentionPolicy.RUNTIME)
@@ -95,31 +94,32 @@
9594
public @interface DocumentReference {
9695

9796
/**
98-
* The database the linked entity resides in.
97+
* The database the referenced entity resides in. Uses the default database provided by
98+
* {@link org.springframework.data.mongodb.MongoDatabaseFactory} if empty.
9999
*
100-
* @return empty String by default. Uses the default database provided buy the {@link org.springframework.data.mongodb.MongoDatabaseFactory}.
100+
* @see MongoDatabaseFactory#getMongoDatabase()
101+
* @see MongoDatabaseFactory#getMongoDatabase(String)
101102
*/
102103
String db() default "";
103104

104105
/**
105-
* The database the linked entity resides in.
106+
* The collection the referenced entity resides in. Defaults to the collection of the referenced entity type.
106107
*
107-
* @return empty String by default. Uses the property type for collection resolution.
108+
* @see MongoPersistentEntity#getCollection()
108109
*/
109110
String collection() default "";
110111

111112
/**
112-
* The single document lookup query. In case of an {@link java.util.Collection} or {@link java.util.Map} property
113-
* the individual lookups are combined via an `$or` operator.
113+
* The single document lookup query. In case of an {@link java.util.Collection} or {@link java.util.Map} property the
114+
* individual lookups are combined via an {@code $or} operator. {@code target} points to the source value (or
115+
* document) stored at the reference property. Properties of {@code target} can be used to define the reference query.
114116
*
115117
* @return an {@literal _id} based lookup.
116118
*/
117119
String lookup() default "{ '_id' : ?#{#target} }";
118120

119121
/**
120122
* A specific sort.
121-
*
122-
* @return empty String by default.
123123
*/
124124
String sort() default "";
125125

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/MongoTransactionManagerUnitTests.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.mockito.Mock;
2626
import org.mockito.junit.jupiter.MockitoExtension;
2727

28+
import org.springframework.data.mongodb.core.MongoExceptionTranslator;
2829
import org.springframework.data.mongodb.core.MongoTemplate;
2930
import org.springframework.transaction.TransactionDefinition;
3031
import org.springframework.transaction.TransactionStatus;
@@ -37,12 +38,15 @@
3738
import com.mongodb.client.ClientSession;
3839
import com.mongodb.client.MongoDatabase;
3940
import com.mongodb.session.ServerSession;
41+
import org.mockito.junit.jupiter.MockitoSettings;
42+
import org.mockito.quality.Strictness;
4043

4144
/**
4245
* @author Christoph Strobl
4346
*/
4447
@ExtendWith(MockitoExtension.class)
45-
public class MongoTransactionManagerUnitTests {
48+
@MockitoSettings(strictness = Strictness.LENIENT)
49+
class MongoTransactionManagerUnitTests {
4650

4751
@Mock ClientSession session;
4852
@Mock ClientSession session2;
@@ -53,23 +57,25 @@ public class MongoTransactionManagerUnitTests {
5357
@Mock MongoDatabase db2;
5458

5559
@BeforeEach
56-
public void setUp() {
60+
void setUp() {
5761

5862
when(dbFactory.getSession(any())).thenReturn(session, session2);
63+
when(dbFactory.getExceptionTranslator()).thenReturn(new MongoExceptionTranslator());
64+
when(dbFactory2.getExceptionTranslator()).thenReturn(new MongoExceptionTranslator());
5965
when(dbFactory.withSession(session)).thenReturn(dbFactory);
6066
when(dbFactory.getMongoDatabase()).thenReturn(db);
6167
when(session.getServerSession()).thenReturn(serverSession);
6268
}
6369

6470
@AfterEach
65-
public void verifyTransactionSynchronizationManager() {
71+
void verifyTransactionSynchronizationManager() {
6672

6773
assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
6874
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
6975
}
7076

7177
@Test // DATAMONGO-1920
72-
public void triggerCommitCorrectly() {
78+
void triggerCommitCorrectly() {
7379

7480
MongoTransactionManager txManager = new MongoTransactionManager(dbFactory);
7581
TransactionStatus txStatus = txManager.getTransaction(new DefaultTransactionDefinition());
@@ -91,7 +97,7 @@ public void triggerCommitCorrectly() {
9197
}
9298

9399
@Test // DATAMONGO-1920
94-
public void participateInOnGoingTransactionWithCommit() {
100+
void participateInOnGoingTransactionWithCommit() {
95101

96102
MongoTransactionManager txManager = new MongoTransactionManager(dbFactory);
97103
TransactionStatus txStatus = txManager.getTransaction(new DefaultTransactionDefinition());
@@ -126,7 +132,7 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
126132
}
127133

128134
@Test // DATAMONGO-1920
129-
public void participateInOnGoingTransactionWithRollbackOnly() {
135+
void participateInOnGoingTransactionWithRollbackOnly() {
130136

131137
MongoTransactionManager txManager = new MongoTransactionManager(dbFactory);
132138
TransactionStatus txStatus = txManager.getTransaction(new DefaultTransactionDefinition());
@@ -163,7 +169,7 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
163169
}
164170

165171
@Test // DATAMONGO-1920
166-
public void triggerRollbackCorrectly() {
172+
void triggerRollbackCorrectly() {
167173

168174
MongoTransactionManager txManager = new MongoTransactionManager(dbFactory);
169175
TransactionStatus txStatus = txManager.getTransaction(new DefaultTransactionDefinition());
@@ -185,7 +191,7 @@ public void triggerRollbackCorrectly() {
185191
}
186192

187193
@Test // DATAMONGO-1920
188-
public void suspendTransactionWhilePropagationNotSupported() {
194+
void suspendTransactionWhilePropagationNotSupported() {
189195

190196
MongoTransactionManager txManager = new MongoTransactionManager(dbFactory);
191197
TransactionStatus txStatus = txManager.getTransaction(new DefaultTransactionDefinition());
@@ -228,7 +234,7 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
228234
}
229235

230236
@Test // DATAMONGO-1920
231-
public void suspendTransactionWhilePropagationRequiresNew() {
237+
void suspendTransactionWhilePropagationRequiresNew() {
232238

233239
when(dbFactory.withSession(session2)).thenReturn(dbFactory2);
234240
when(dbFactory2.getMongoDatabase()).thenReturn(db2);
@@ -277,7 +283,7 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
277283
}
278284

279285
@Test // DATAMONGO-1920
280-
public void readonlyShouldInitiateASessionStartAndCommitTransaction() {
286+
void readonlyShouldInitiateASessionStartAndCommitTransaction() {
281287

282288
MongoTransactionManager txManager = new MongoTransactionManager(dbFactory);
283289

@@ -303,7 +309,7 @@ public void readonlyShouldInitiateASessionStartAndCommitTransaction() {
303309
}
304310

305311
@Test // DATAMONGO-1920
306-
public void readonlyShouldInitiateASessionStartAndRollbackTransaction() {
312+
void readonlyShouldInitiateASessionStartAndRollbackTransaction() {
307313

308314
MongoTransactionManager txManager = new MongoTransactionManager(dbFactory);
309315

0 commit comments

Comments
 (0)