#81 - Samples for new projection support for JPA and MongoDB.

This commit is contained in:
Oliver Gierke
2015-05-12 11:41:31 +02:00
parent a872e57ed1
commit 9cb44b3166
14 changed files with 631 additions and 2 deletions

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2015 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.jpa.projections;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import lombok.Data;
import lombok.RequiredArgsConstructor;
/**
* @author Oliver Gierke
*/
@Data
@Entity
@RequiredArgsConstructor
public class Customer {
private @GeneratedValue @Id Long id;
private final String firstname, lastname;
protected Customer() {
this.firstname = null;
this.lastname = null;
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2015 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.jpa.projections;
import lombok.Data;
import lombok.RequiredArgsConstructor;
/**
* @author Oliver Gierke
*/
@Data
@RequiredArgsConstructor
public class CustomerDto {
private final String firstname;
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2015 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.jpa.projections;
/**
*
* @author Oliver Gierke
*/
public interface CustomerProjection {
String getFirstname();
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2015 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.jpa.projections;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
/**
* @author Oliver Gierke
*/
public interface CustomerRepository extends CrudRepository<Customer, Long> {
/**
* 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();
/**
* Projection interfaces can be used with manually declared queries, too. Make sure you alias the projects matching
* the projection fields.
*
* @return
*/
@Query("select c.firstname as firstname, c.lastname as lastname from Customer c")
Collection<CustomerProjection> findsByProjectedColumns();
/**
* Uses a concrete DTO type to indicate the fields to be returned. This gets translated into a constructor expression
* in the query.
*
* @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(Long id);
/**
* Dynamic projection for a single entity.
*
* @param id
* @param projection
* @return
*/
<T> T findProjectedById(Long id, Class<T> projection);
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2015 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.jpa.projections;
import org.springframework.beans.factory.annotation.Value;
/**
* @author Oliver Gierke
*/
public interface CustomerSummary {
@Value("#{target.firstname + ' ' + target.lastname}")
String getFullName();
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright 2015 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.jpa.projections;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.Collection;
import java.util.Map;
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.context.annotation.Configuration;
import org.springframework.data.projection.TargetAware;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Integaration tests for {@link CustomerRepository} to show projection capabilities.
*
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Transactional
public class CustomerRepositoryIntegrationTest {
@Configuration
@EnableAutoConfiguration
static class Config {}
@Autowired CustomerRepository customers;
Customer dave, carter;
@Before
public void setUp() {
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 projectsMapIntoInterface() {
Collection<CustomerProjection> result = customers.findsByProjectedColumns();
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.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 projectedDave = customers.findProjectedById(dave.getId());
assertThat(projectedDave.getFirstname(), is("Dave"));
assertThat(((TargetAware) projectedDave).getTarget(), is(instanceOf(Map.class)));
}
}

View File

@@ -20,7 +20,7 @@
<version>${apt.version}</version>
<dependencies>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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();
}

View File

@@ -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);
}

View File

@@ -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();
}

View File

@@ -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)));
}
}

View File

@@ -33,7 +33,7 @@
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-mongodb</artifactId>
<version>${querydsl.version}</version>
</dependency>