diff --git a/jdbc/singlequeryloading/pom.xml b/jdbc/singlequeryloading/pom.xml index 4c174a1d1..c24bd8e13 100644 --- a/jdbc/singlequeryloading/pom.xml +++ b/jdbc/singlequeryloading/pom.xml @@ -1,41 +1,33 @@ - - - 4.0.0 + + 4.0.0 - singlequeryloading - - 21 - 21 - + singlequeryloading - - org.springframework.data.examples - spring-data-jdbc-examples - 2.0.0.BUILD-SNAPSHOT - ../pom.xml - + + org.springframework.data.examples + spring-data-jdbc-examples + 2.0.0.BUILD-SNAPSHOT + ../pom.xml + + Spring Data JDBC - Demonstration of Single Query Loading + Sample project demonstrating Single Query Loading with Spring Data JDBC + - Spring Data JDBC - Demonstration of Single Query Loading - Sample project demonstrating Single Query Loading with Spring Data JDBC - - - - org.springframework.boot - spring-boot-starter-data-jdbc - - - org.springframework - spring-test - - - org.testcontainers - postgresql - - - org.postgresql - postgresql - - + + + org.springframework.boot + spring-boot-starter-data-jdbc + + + org.testcontainers + postgresql + + + org.postgresql + postgresql + + diff --git a/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/Config.java b/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/Config.java index 4bcf284fb..b23133d84 100644 --- a/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/Config.java +++ b/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/Config.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -15,6 +15,8 @@ */ package example.springdata.jdbc.singlequeryloading; +import java.util.Optional; + import org.springframework.boot.SpringBootConfiguration; import org.springframework.data.jdbc.core.convert.JdbcCustomConversions; import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; @@ -23,8 +25,6 @@ import org.springframework.data.relational.RelationalManagedTypes; import org.springframework.data.relational.core.mapping.NamingStrategy; -import java.util.Optional; - /** * Spring application context configuration that enables Single Query Loading. * @@ -34,11 +34,12 @@ @EnableJdbcRepositories public class Config extends AbstractJdbcConfiguration { - @Override - public JdbcMappingContext jdbcMappingContext(Optional namingStrategy, JdbcCustomConversions customConversions, RelationalManagedTypes jdbcManagedTypes) { + public JdbcMappingContext jdbcMappingContext(Optional namingStrategy, + JdbcCustomConversions customConversions, RelationalManagedTypes jdbcManagedTypes) { - JdbcMappingContext jdbcMappingContext = super.jdbcMappingContext(namingStrategy, customConversions, jdbcManagedTypes); + JdbcMappingContext jdbcMappingContext = super.jdbcMappingContext(namingStrategy, customConversions, + jdbcManagedTypes); jdbcMappingContext.setSingleQueryLoadingEnabled(true); return jdbcMappingContext; } diff --git a/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwner.java b/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwner.java index 8eb9c1099..bad882cc0 100644 --- a/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwner.java +++ b/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwner.java @@ -5,21 +5,21 @@ * 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 + * 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.jdbc.singlequeryloading; - -import org.springframework.data.annotation.Id; + */ +package example.springdata.jdbc.singlequeryloading; -import java.util.ArrayList; import java.util.List; import java.util.Objects; +import org.springframework.data.annotation.Id; + /** * An aggregate with mutliple collections. * @@ -27,16 +27,15 @@ */ class PetOwner { - @Id - Long Id; + @Id Long Id; String name; - List dogs = new ArrayList<>(); + List dogs; - List cats = new ArrayList<>(); + List cats; - List fish = new ArrayList<>(); + List fish; public PetOwner(String name, List cats, List dogs, List fish) { @@ -48,10 +47,13 @@ public PetOwner(String name, List cats, List dogs, List fish) { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; PetOwner petOwner = (PetOwner) o; - return Objects.equals(Id, petOwner.Id) && Objects.equals(name, petOwner.name) && Objects.equals(dogs, petOwner.dogs) && Objects.equals(cats, petOwner.cats) && Objects.equals(fish, petOwner.fish); + return Objects.equals(Id, petOwner.Id) && Objects.equals(name, petOwner.name) && Objects.equals(dogs, petOwner.dogs) + && Objects.equals(cats, petOwner.cats) && Objects.equals(fish, petOwner.fish); } @Override diff --git a/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwnerRepository.java b/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwnerRepository.java index 8fd2d4c6c..00f2084f4 100644 --- a/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwnerRepository.java +++ b/jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwnerRepository.java @@ -5,25 +5,21 @@ * 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 + * 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.jdbc.singlequeryloading; - -import org.springframework.data.repository.ListCrudRepository; - -import java.util.List; + */ +package example.springdata.jdbc.singlequeryloading; +import org.springframework.data.repository.CrudRepository; /** * Repository to access {@link PetOwner} instances. * * @author Jens Schauder */ -interface PetOwnerRepository extends ListCrudRepository { - List findByName(String marry); -} +interface PetOwnerRepository extends CrudRepository {} diff --git a/jdbc/singlequeryloading/src/test/java/example/springdata/jdbc/singlequeryloading/SingleQueryLoadingApplicationTests.java b/jdbc/singlequeryloading/src/test/java/example/springdata/jdbc/singlequeryloading/SingleQueryLoadingApplicationTests.java index 73768f8fa..6733eca4e 100644 --- a/jdbc/singlequeryloading/src/test/java/example/springdata/jdbc/singlequeryloading/SingleQueryLoadingApplicationTests.java +++ b/jdbc/singlequeryloading/src/test/java/example/springdata/jdbc/singlequeryloading/SingleQueryLoadingApplicationTests.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -15,19 +15,19 @@ */ package example.springdata.jdbc.singlequeryloading; +import static org.assertj.core.api.Assertions.*; +import static org.springframework.data.relational.core.query.Criteria.*; +import static org.springframework.data.relational.core.query.Query.*; + +import java.util.List; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; import org.springframework.data.jdbc.core.JdbcAggregateTemplate; -import org.springframework.data.relational.core.query.Criteria; -import org.springframework.data.relational.core.query.CriteriaDefinition; -import org.springframework.data.relational.core.query.Query; - -import java.util.List; - -import static org.assertj.core.api.Assertions.*; /** * Run tests demonstrating the use of Single Query Loading. You'll have to observe the executed queries. @@ -38,52 +38,26 @@ @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) class SingleQueryLoadingApplicationTests { - @Autowired - PetOwnerRepository petOwners; - @Autowired - JdbcAggregateTemplate template; + @Autowired PetOwnerRepository petOwners; + @Autowired JdbcAggregateTemplate template; + private PetOwner emil; private PetOwner marry; @BeforeEach void setup() { + petOwners.deleteAll(); - emil = petOwners.save(new PetOwner("Emil", - List.of( - new Cat("Edgar"), - new Cat("Einstein"), - new Cat("Elliot"), - new Cat("Elton"), - new Cat("Evan") - ), - List.of( - new Dog("Eric"), - new Dog("Eddie"), - new Dog("Eke"), - new Dog("Echo") - ), - List.of( - new Fish("Floaty") - ) + emil = petOwners.save(new PetOwner("Emil", // + List.of(new Cat("Edgar"), new Cat("Einstein"), new Cat("Elliot"), new Cat("Elton"), new Cat("Evan")), // + List.of(new Dog("Eric"), new Dog("Eddie"), new Dog("Eke"), new Dog("Echo")), // + List.of(new Fish("Floaty Mc Floatface")) // )); - marry = petOwners.save(new PetOwner("Marry", - List.of( - new Cat("Mars"), - new Cat("Maverick"), - new Cat("Max") - ), - List.of( - new Dog("Molly"), - new Dog("Murphy"), - new Dog("Madison"), - new Dog("Macie") - ), - List.of( - new Fish("Mahi Mahi"), - new Fish("Mr. Limpet") - ) + marry = petOwners.save(new PetOwner("Marry", List.of(new Cat("Mars"), new Cat("Maverick"), new Cat("Max")), // + List.of(new Dog("Molly"), new Dog("Murphy"), new Dog("Madison"), new Dog("Macie")), // + List.of(new Fish("Mahi Mahi"), new Fish("Mr. Limpet")) // )); } @@ -93,15 +67,13 @@ void loadById() { PetOwner emilReloaded = petOwners.findById(emil.Id).orElseThrow(); assertThat(emilReloaded).isEqualTo(emil); - } @Test - void loadByName() { + void loadByNameUsingTemplate() { + + List marries = (List) template.findAll(query(where("name").is("Marry")), PetOwner.class); - CriteriaDefinition criteria = Criteria.where("name").is("Marry"); - Query query = Query.query(criteria); - List marries = (List) template.findAll(query, PetOwner.class); assertThat(marries).containsExactly(marry); }