Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Reformat code. Simplify config.

See #675
  • Loading branch information
mp911de committed Nov 7, 2023
1 parent 7c696ac commit 529fc71
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 114 deletions.
64 changes: 28 additions & 36 deletions jdbc/singlequeryloading/pom.xml
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>singlequeryloading</artifactId>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<artifactId>singlequeryloading</artifactId>

<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jdbc-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jdbc-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<name>Spring Data JDBC - Demonstration of Single Query Loading</name>
<description>Sample project demonstrating Single Query Loading with Spring Data JDBC
</description>

<name>Spring Data JDBC - Demonstration of Single Query Loading</name>
<description>Sample project demonstrating Single Query Loading with Spring Data JDBC</description>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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.
*
Expand All @@ -34,11 +34,12 @@
@EnableJdbcRepositories
public class Config extends AbstractJdbcConfiguration {


@Override
public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy, JdbcCustomConversions customConversions, RelationalManagedTypes jdbcManagedTypes) {
public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy,
JdbcCustomConversions customConversions, RelationalManagedTypes jdbcManagedTypes) {

JdbcMappingContext jdbcMappingContext = super.jdbcMappingContext(namingStrategy, customConversions, jdbcManagedTypes);
JdbcMappingContext jdbcMappingContext = super.jdbcMappingContext(namingStrategy, customConversions,
jdbcManagedTypes);
jdbcMappingContext.setSingleQueryLoadingEnabled(true);
return jdbcMappingContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,37 @@
* 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.
*
* @author Jens Schauder
*/
class PetOwner {

@Id
Long Id;
@Id Long Id;

String name;

List<Dog> dogs = new ArrayList<>();
List<Dog> dogs;

List<Cat> cats = new ArrayList<>();
List<Cat> cats;

List<Fish> fish = new ArrayList<>();
List<Fish> fish;

public PetOwner(String name, List<Cat> cats, List<Dog> dogs, List<Fish> fish) {

Expand All @@ -48,10 +47,13 @@ public PetOwner(String name, List<Cat> cats, List<Dog> dogs, List<Fish> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PetOwner, Long> {
List<PetOwner> findByName(String marry);
}
interface PetOwnerRepository extends CrudRepository<PetOwner, Long> {}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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")) //
));
}

Expand All @@ -93,15 +67,13 @@ void loadById() {
PetOwner emilReloaded = petOwners.findById(emil.Id).orElseThrow();

assertThat(emilReloaded).isEqualTo(emil);

}

@Test
void loadByName() {
void loadByNameUsingTemplate() {

List<PetOwner> marries = (List<PetOwner>) template.findAll(query(where("name").is("Marry")), PetOwner.class);

CriteriaDefinition criteria = Criteria.where("name").is("Marry");
Query query = Query.query(criteria);
List<PetOwner> marries = (List<PetOwner>) template.findAll(query, PetOwner.class);
assertThat(marries).containsExactly(marry);
}

Expand Down

0 comments on commit 529fc71

Please sign in to comment.