#81 - Samples for new projection support for JPA and MongoDB.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2015-2016 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 example.springdata.mongodb.projections;
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Value
|
||||
@Document
|
||||
class Customer {
|
||||
|
||||
@Id ObjectId id = new ObjectId();
|
||||
String firstname, lastname;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2015-2016 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 example.springdata.mongodb.projections;
|
||||
|
||||
import lombok.Value;
|
||||
|
||||
/**
|
||||
* A sample DTO only containing the firstname.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Value
|
||||
class CustomerDto {
|
||||
|
||||
String firstname;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2015-2016 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 example.springdata.mongodb.projections;
|
||||
|
||||
/**
|
||||
* An example projection interface containing only the firstname.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
interface CustomerProjection {
|
||||
|
||||
String getFirstname();
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2015-2016 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 example.springdata.mongodb.projections;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
* Sample repository managing customers to show projecting functionality of Spring Data MongoDB.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
interface CustomerRepository extends CrudRepository<Customer, ObjectId> {
|
||||
|
||||
/**
|
||||
* Uses a projection interface to indicate the fields to be returned. As the projection doesn't use any dynamic
|
||||
* fields, the query execution will be restricted to only the fields needed by the projection.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Collection<CustomerProjection> findAllProjectedBy();
|
||||
|
||||
/**
|
||||
* When a projection is used that contains dynamic properties (i.e. SpEL expressions in an {@link Value} annotation),
|
||||
* the normal target entity will be loaded but dynamically projected so that the target can be referred to in the
|
||||
* expression.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Collection<CustomerSummary> findAllSummarizedBy();
|
||||
|
||||
/**
|
||||
* Uses a concrete DTO type to indicate the fields to be returned. This will cause the original object being loaded
|
||||
* and the properties copied over into the DTO.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
Collection<CustomerDto> findAllDtoedBy();
|
||||
|
||||
/**
|
||||
* Passes in the projection type dynamically (either interface or DTO).
|
||||
*
|
||||
* @param firstname
|
||||
* @param projection
|
||||
* @return
|
||||
*/
|
||||
<T> Collection<T> findByFirstname(String firstname, Class<T> projection);
|
||||
|
||||
/**
|
||||
* Projection for a single entity.
|
||||
*
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
CustomerProjection findProjectedById(ObjectId id);
|
||||
|
||||
/**
|
||||
* Dynamic projection for a single entity.
|
||||
*
|
||||
* @param id
|
||||
* @param projection
|
||||
* @return
|
||||
*/
|
||||
<T> T findProjectedById(ObjectId id, Class<T> projection);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2015-2016 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 example.springdata.mongodb.projections;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
interface CustomerSummary {
|
||||
|
||||
@Value("#{target.firstname + ' ' + target.lastname}")
|
||||
String getFullName();
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright 2015-2016 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 example.springdata.mongodb.projections;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.projection.TargetAware;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link CustomerRepository} to show projection capabilities.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration
|
||||
@Transactional
|
||||
public class CustomerRepositoryIntegrationTest {
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
static class Config {}
|
||||
|
||||
@Autowired CustomerRepository customers;
|
||||
|
||||
Customer dave, carter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
customers.deleteAll();
|
||||
|
||||
this.dave = customers.save(new Customer("Dave", "Matthews"));
|
||||
this.carter = customers.save(new Customer("Carter", "Beauford"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectsEntityIntoInterface() {
|
||||
|
||||
Collection<CustomerProjection> result = customers.findAllProjectedBy();
|
||||
|
||||
assertThat(result, hasSize(2));
|
||||
assertThat(result.iterator().next().getFirstname(), is("Dave"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectsToDto() {
|
||||
|
||||
Collection<CustomerDto> result = customers.findAllDtoedBy();
|
||||
|
||||
assertThat(result, hasSize(2));
|
||||
assertThat(result.iterator().next().getFirstname(), is("Dave"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectsDynamically() {
|
||||
|
||||
Collection<CustomerProjection> result = customers.findByFirstname("Dave", CustomerProjection.class);
|
||||
|
||||
assertThat(result, hasSize(1));
|
||||
assertThat(result.iterator().next().getFirstname(), is("Dave"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectsIndividualDynamically() {
|
||||
|
||||
CustomerSummary result = customers.findProjectedById(dave.getId(), CustomerSummary.class);
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertThat(result.getFullName(), is("Dave Matthews"));
|
||||
|
||||
// Proxy backed by original instance as the projection uses dynamic elements
|
||||
assertThat(((TargetAware) result).getTarget(), is(instanceOf(Customer.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void projectIndividualInstance() {
|
||||
|
||||
CustomerProjection result = customers.findProjectedById(dave.getId());
|
||||
|
||||
assertThat(result, is(notNullValue()));
|
||||
assertThat(result.getFirstname(), is("Dave"));
|
||||
assertThat(((TargetAware) result).getTarget(), is(instanceOf(Customer.class)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user