Browse Source
Introduced DbRefResolver interface in order to be able to abstract how a DbRef is resolved that is used in MappingMongoConverter#doWithAssociations. The present behaviour was to resolve a DbRef eagerly. This functionality is now implemented by EagerDbRefResolver. In order to support lazy loading we have to provide some means to define the desired loading behaviour. This can now be done via the "lazy"-Attribute on @DbRef which defaults to false. If the attribute is set to true the LazyDbRefResolver is used to create a Proxy that eagerly loads the required data on demand when one of the (non-Object) proxy methods is called. MongoDbFactory now exposes a MongoExceptionTranslator that is now used by the MappingMongoConverter and MongoTemplate. Introduced a DelegatingDbRefResolver that can delegate to a DbRefResolveCallback in order to perform the actual DbRef resolution. We now use cglib-proxies if necessary if the referenced association is a concrete class. Added unit tests for lazy loading of interface types, concrete collection types and concrete domain types. Exposed state from LazyLoadingInterceptor for better testability. Added unit tests for lazy loading of classes with custom PersistenceConstructor. Moved integration tests for PersonRepository into its own test class.pull/92/head
13 changed files with 763 additions and 37 deletions
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
/* |
||||
* Copyright 2013 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 |
||||
* |
||||
* 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 org.springframework.data.mongodb.core.convert; |
||||
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; |
||||
|
||||
/** |
||||
* Callback interface to be used in conjunction with {@link DbRefResolver}. |
||||
* |
||||
* @author Thomas Darimont |
||||
*/ |
||||
interface DbRefResolveCallback { |
||||
|
||||
/** |
||||
* @param property |
||||
* @return |
||||
*/ |
||||
Object resolve(MongoPersistentProperty property); |
||||
} |
||||
@ -0,0 +1,33 @@
@@ -0,0 +1,33 @@
|
||||
/* |
||||
* Copyright 2013 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 |
||||
* |
||||
* 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 org.springframework.data.mongodb.core.convert; |
||||
|
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; |
||||
|
||||
/** |
||||
* Used to resolve associations annotated with {@link org.springframework.data.mongodb.core.mapping.DBRef}. |
||||
* |
||||
* @author Thomas Darimont |
||||
*/ |
||||
interface DbRefResolver { |
||||
|
||||
/** |
||||
* @param property |
||||
* @param callback |
||||
* @return |
||||
*/ |
||||
Object resolve(MongoPersistentProperty property, DbRefResolveCallback callback); |
||||
} |
||||
@ -0,0 +1,151 @@
@@ -0,0 +1,151 @@
|
||||
/* |
||||
* Copyright 2010-2013 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 |
||||
* |
||||
* 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 org.springframework.data.mongodb.repository; |
||||
|
||||
import static org.hamcrest.Matchers.*; |
||||
import static org.junit.Assert.*; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.springframework.aop.Advisor; |
||||
import org.springframework.aop.framework.Advised; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.data.mongodb.core.MongoOperations; |
||||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter.LazyLoadingInterceptor; |
||||
import org.springframework.test.context.ContextConfiguration; |
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||
|
||||
/** |
||||
* Integration test for {@link PersonRepository} for lazy loading support. |
||||
* |
||||
* @author Thomas Darimont |
||||
*/ |
||||
@ContextConfiguration(locations = "PersonRepositoryIntegrationTests-context.xml") |
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
public class PersonRepositoryLazyLoadingIntegrationTests { |
||||
|
||||
@Autowired PersonRepository repository; |
||||
|
||||
@Autowired MongoOperations operations; |
||||
|
||||
@Before |
||||
public void setUp() throws InterruptedException { |
||||
|
||||
repository.deleteAll(); |
||||
operations.remove(new org.springframework.data.mongodb.core.query.Query(), User.class); |
||||
} |
||||
|
||||
/** |
||||
* @see DATAMONGO-348 |
||||
*/ |
||||
@Test |
||||
public void shouldLoadAssociationWithDbRefOnInterfaceAndLazyLoadingEnabled() throws Exception { |
||||
|
||||
User thomas = new User(); |
||||
thomas.username = "Thomas"; |
||||
operations.save(thomas); |
||||
|
||||
Person person = new Person(); |
||||
person.setFirstname("Oliver"); |
||||
person.setFans(Arrays.asList(thomas)); |
||||
person.setRealFans(new ArrayList<User>(Arrays.asList(thomas))); |
||||
repository.save(person); |
||||
|
||||
Person oliver = repository.findOne(person.id); |
||||
List<User> fans = oliver.getFans(); |
||||
LazyLoadingInterceptor interceptor = extractInterceptor(fans); |
||||
|
||||
assertThat(interceptor.getResult(), is(nullValue())); |
||||
assertThat(interceptor.isResolved(), is(false)); |
||||
|
||||
User user = fans.get(0); |
||||
assertThat(interceptor.getResult(), is(notNullValue())); |
||||
assertThat(interceptor.isResolved(), is(true)); |
||||
assertThat(user.getUsername(), is(thomas.getUsername())); |
||||
} |
||||
|
||||
/** |
||||
* @see DATAMONGO-348 |
||||
*/ |
||||
@Test |
||||
public void shouldLoadAssociationWithDbRefOnConcreteCollectionAndLazyLoadingEnabled() throws Exception { |
||||
|
||||
User thomas = new User(); |
||||
thomas.username = "Thomas"; |
||||
operations.save(thomas); |
||||
|
||||
Person person = new Person(); |
||||
person.setFirstname("Oliver"); |
||||
person.setFans(Arrays.asList(thomas)); |
||||
person.setRealFans(new ArrayList<User>(Arrays.asList(thomas))); |
||||
repository.save(person); |
||||
|
||||
Person oliver = repository.findOne(person.id); |
||||
List<User> realFans = oliver.getRealFans(); |
||||
LazyLoadingInterceptor interceptor = extractInterceptor(realFans); |
||||
|
||||
assertThat(interceptor.getResult(), is(nullValue())); |
||||
assertThat(interceptor.isResolved(), is(false)); |
||||
|
||||
User realFan = realFans.get(0); |
||||
assertThat(interceptor.getResult(), is(notNullValue())); |
||||
assertThat(interceptor.isResolved(), is(true)); |
||||
assertThat(realFan.getUsername(), is(thomas.getUsername())); |
||||
|
||||
realFans = oliver.getRealFans(); |
||||
assertThat(interceptor.getResult(), is(notNullValue())); |
||||
assertThat(interceptor.isResolved(), is(true)); |
||||
|
||||
realFan = realFans.get(0); |
||||
assertThat(realFan.getUsername(), is(thomas.getUsername())); |
||||
} |
||||
|
||||
/** |
||||
* @see DATAMONGO-348 |
||||
*/ |
||||
@Test |
||||
public void shouldLoadAssociationWithDbRefOnConcreteDomainClassAndLazyLoadingEnabled() throws Exception { |
||||
|
||||
User thomas = new User(); |
||||
thomas.username = "Thomas"; |
||||
operations.save(thomas); |
||||
|
||||
Person person = new Person(); |
||||
person.setFirstname("Oliver"); |
||||
person.setCoworker(thomas); |
||||
repository.save(person); |
||||
|
||||
Person oliver = repository.findOne(person.id); |
||||
|
||||
User coworker = oliver.getCoworker(); |
||||
LazyLoadingInterceptor interceptor = extractInterceptor(coworker); |
||||
|
||||
assertThat(interceptor.getResult(), is(nullValue())); |
||||
assertThat(interceptor.isResolved(), is(false)); |
||||
assertThat(coworker.getUsername(), is(thomas.getUsername())); |
||||
assertThat(interceptor.isResolved(), is(true)); |
||||
assertThat(coworker.getUsername(), is(thomas.getUsername())); |
||||
} |
||||
|
||||
private LazyLoadingInterceptor extractInterceptor(Object proxy) { |
||||
return (LazyLoadingInterceptor) ((Advisor) ((Advised) proxy).getAdvisors()[0]).getAdvice(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue