-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance support for linking entities.
Add initial support for an alternative to the existing DBRef scenario. The enhancement allows to store and retrieve linked entites via their id or a customizable lookup query. Original pull request: #3647. Closes #3602.
- Loading branch information
1 parent
f1354c4
commit a51c962
Showing
18 changed files
with
1,779 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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 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
71 changes: 71 additions & 0 deletions
71
...b/src/main/java/org/springframework/data/mongodb/core/convert/DefaultReferenceLoader.java
This file contains 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,71 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.data.mongodb.core.convert; | ||
|
||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
import org.bson.Document; | ||
import org.bson.conversions.Bson; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.data.mongodb.MongoDatabaseFactory; | ||
import org.springframework.data.mongodb.MongoDatabaseUtils; | ||
import org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceContext; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
import com.mongodb.client.FindIterable; | ||
import com.mongodb.client.MongoCollection; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
*/ | ||
public class DefaultReferenceLoader implements ReferenceLoader { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultReferenceLoader.class); | ||
|
||
private final MongoDatabaseFactory mongoDbFactory; | ||
|
||
public DefaultReferenceLoader(MongoDatabaseFactory mongoDbFactory) { | ||
|
||
Assert.notNull(mongoDbFactory, "MongoDbFactory translator must not be null!"); | ||
|
||
this.mongoDbFactory = mongoDbFactory; | ||
} | ||
|
||
@Override | ||
public Stream<Document> bulkFetch(ReferenceFilter filter, ReferenceContext context) { | ||
|
||
MongoCollection<Document> collection = getCollection(context); | ||
|
||
if (LOGGER.isTraceEnabled()) { | ||
LOGGER.trace("Bulk fetching {} from {}.{}.", filter, | ||
StringUtils.hasText(context.getDatabase()) ? context.getDatabase() | ||
: collection.getNamespace().getDatabaseName(), | ||
context.getCollection()); | ||
} | ||
|
||
return filter.apply(collection); | ||
} | ||
|
||
protected MongoCollection<Document> getCollection(ReferenceContext context) { | ||
|
||
return MongoDatabaseUtils.getDatabase(context.database, mongoDbFactory).getCollection(context.collection, | ||
Document.class); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...src/main/java/org/springframework/data/mongodb/core/convert/DefaultReferenceResolver.java
This file contains 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,69 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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 org.springframework.data.mongodb.core.convert; | ||
|
||
import java.util.function.BiFunction; | ||
import java.util.stream.Stream; | ||
|
||
import org.bson.Document; | ||
import org.bson.conversions.Bson; | ||
import org.springframework.data.mongodb.core.convert.ReferenceLoader.ReferenceFilter; | ||
import org.springframework.data.mongodb.core.mapping.DocumentReference; | ||
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; | ||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
*/ | ||
public class DefaultReferenceResolver implements ReferenceResolver { | ||
|
||
private final ReferenceLoader referenceLoader; | ||
|
||
public DefaultReferenceResolver(ReferenceLoader referenceLoader) { | ||
this.referenceLoader = referenceLoader; | ||
} | ||
|
||
@Override | ||
public ReferenceLoader getReferenceLoader() { | ||
return referenceLoader; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Object resolveReference(MongoPersistentProperty property, Object source, ReferenceReader referenceReader, | ||
BiFunction<ReferenceContext, ReferenceFilter, Stream<Document>> lookupFunction) { | ||
|
||
if (isLazyReference(property)) { | ||
return createLazyLoadingProxy(property, source, referenceReader, lookupFunction); | ||
} | ||
|
||
return referenceReader.readReference(property, source, lookupFunction); | ||
} | ||
|
||
private Object createLazyLoadingProxy(MongoPersistentProperty property, Object source, | ||
ReferenceReader referenceReader, BiFunction<ReferenceContext, ReferenceFilter, Stream<Document>> lookupFunction) { | ||
return new LazyLoadingProxyGenerator(referenceReader).createLazyLoadingProxy(property, source, lookupFunction); | ||
} | ||
|
||
protected boolean isLazyReference(MongoPersistentProperty property) { | ||
|
||
if (property.findAnnotation(DocumentReference.class) != null) { | ||
return property.findAnnotation(DocumentReference.class).lazy(); | ||
} | ||
|
||
return property.getDBRef() != null && property.getDBRef().lazy(); | ||
} | ||
} |
Oops, something went wrong.