Skip to content

Commit 529fc71

Browse files
committed
Polishing.
Reformat code. Simplify config. See #675
1 parent 7c696ac commit 529fc71

File tree

5 files changed

+77
-114
lines changed

5 files changed

+77
-114
lines changed

jdbc/singlequeryloading/pom.xml

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,33 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
55

6-
<artifactId>singlequeryloading</artifactId>
7-
<properties>
8-
<maven.compiler.source>21</maven.compiler.source>
9-
<maven.compiler.target>21</maven.compiler.target>
10-
</properties>
6+
<artifactId>singlequeryloading</artifactId>
117

12-
<parent>
13-
<groupId>org.springframework.data.examples</groupId>
14-
<artifactId>spring-data-jdbc-examples</artifactId>
15-
<version>2.0.0.BUILD-SNAPSHOT</version>
16-
<relativePath>../pom.xml</relativePath>
17-
</parent>
8+
<parent>
9+
<groupId>org.springframework.data.examples</groupId>
10+
<artifactId>spring-data-jdbc-examples</artifactId>
11+
<version>2.0.0.BUILD-SNAPSHOT</version>
12+
<relativePath>../pom.xml</relativePath>
13+
</parent>
1814

15+
<name>Spring Data JDBC - Demonstration of Single Query Loading</name>
16+
<description>Sample project demonstrating Single Query Loading with Spring Data JDBC
17+
</description>
1918

20-
<name>Spring Data JDBC - Demonstration of Single Query Loading</name>
21-
<description>Sample project demonstrating Single Query Loading with Spring Data JDBC</description>
22-
23-
<dependencies>
24-
<dependency>
25-
<groupId>org.springframework.boot</groupId>
26-
<artifactId>spring-boot-starter-data-jdbc</artifactId>
27-
</dependency>
28-
<dependency>
29-
<groupId>org.springframework</groupId>
30-
<artifactId>spring-test</artifactId>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.testcontainers</groupId>
34-
<artifactId>postgresql</artifactId>
35-
</dependency>
36-
<dependency>
37-
<groupId>org.postgresql</groupId>
38-
<artifactId>postgresql</artifactId>
39-
</dependency>
40-
</dependencies>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-data-jdbc</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.testcontainers</groupId>
26+
<artifactId>postgresql</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.postgresql</groupId>
30+
<artifactId>postgresql</artifactId>
31+
</dependency>
32+
</dependencies>
4133
</project>

jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/Config.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,6 +15,8 @@
1515
*/
1616
package example.springdata.jdbc.singlequeryloading;
1717

18+
import java.util.Optional;
19+
1820
import org.springframework.boot.SpringBootConfiguration;
1921
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
2022
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
@@ -23,8 +25,6 @@
2325
import org.springframework.data.relational.RelationalManagedTypes;
2426
import org.springframework.data.relational.core.mapping.NamingStrategy;
2527

26-
import java.util.Optional;
27-
2828
/**
2929
* Spring application context configuration that enables Single Query Loading.
3030
*
@@ -34,11 +34,12 @@
3434
@EnableJdbcRepositories
3535
public class Config extends AbstractJdbcConfiguration {
3636

37-
3837
@Override
39-
public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy, JdbcCustomConversions customConversions, RelationalManagedTypes jdbcManagedTypes) {
38+
public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy,
39+
JdbcCustomConversions customConversions, RelationalManagedTypes jdbcManagedTypes) {
4040

41-
JdbcMappingContext jdbcMappingContext = super.jdbcMappingContext(namingStrategy, customConversions, jdbcManagedTypes);
41+
JdbcMappingContext jdbcMappingContext = super.jdbcMappingContext(namingStrategy, customConversions,
42+
jdbcManagedTypes);
4243
jdbcMappingContext.setSingleQueryLoadingEnabled(true);
4344
return jdbcMappingContext;
4445
}

jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwner.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,37 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/package example.springdata.jdbc.singlequeryloading;
16-
17-
import org.springframework.data.annotation.Id;
15+
*/
16+
package example.springdata.jdbc.singlequeryloading;
1817

19-
import java.util.ArrayList;
2018
import java.util.List;
2119
import java.util.Objects;
2220

21+
import org.springframework.data.annotation.Id;
22+
2323
/**
2424
* An aggregate with mutliple collections.
2525
*
2626
* @author Jens Schauder
2727
*/
2828
class PetOwner {
2929

30-
@Id
31-
Long Id;
30+
@Id Long Id;
3231

3332
String name;
3433

35-
List<Dog> dogs = new ArrayList<>();
34+
List<Dog> dogs;
3635

37-
List<Cat> cats = new ArrayList<>();
36+
List<Cat> cats;
3837

39-
List<Fish> fish = new ArrayList<>();
38+
List<Fish> fish;
4039

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

@@ -48,10 +47,13 @@ public PetOwner(String name, List<Cat> cats, List<Dog> dogs, List<Fish> fish) {
4847

4948
@Override
5049
public boolean equals(Object o) {
51-
if (this == o) return true;
52-
if (o == null || getClass() != o.getClass()) return false;
50+
if (this == o)
51+
return true;
52+
if (o == null || getClass() != o.getClass())
53+
return false;
5354
PetOwner petOwner = (PetOwner) o;
54-
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);
55+
return Objects.equals(Id, petOwner.Id) && Objects.equals(name, petOwner.name) && Objects.equals(dogs, petOwner.dogs)
56+
&& Objects.equals(cats, petOwner.cats) && Objects.equals(fish, petOwner.fish);
5557
}
5658

5759
@Override

jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwnerRepository.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,21 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/package example.springdata.jdbc.singlequeryloading;
16-
17-
import org.springframework.data.repository.ListCrudRepository;
18-
19-
import java.util.List;
15+
*/
16+
package example.springdata.jdbc.singlequeryloading;
2017

18+
import org.springframework.data.repository.CrudRepository;
2119

2220
/**
2321
* Repository to access {@link PetOwner} instances.
2422
*
2523
* @author Jens Schauder
2624
*/
27-
interface PetOwnerRepository extends ListCrudRepository<PetOwner, Long> {
28-
List<PetOwner> findByName(String marry);
29-
}
25+
interface PetOwnerRepository extends CrudRepository<PetOwner, Long> {}

jdbc/singlequeryloading/src/test/java/example/springdata/jdbc/singlequeryloading/SingleQueryLoadingApplicationTests.java

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,19 +15,19 @@
1515
*/
1616
package example.springdata.jdbc.singlequeryloading;
1717

18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.relational.core.query.Criteria.*;
20+
import static org.springframework.data.relational.core.query.Query.*;
21+
22+
import java.util.List;
23+
1824
import org.junit.jupiter.api.BeforeEach;
1925
import org.junit.jupiter.api.Test;
26+
2027
import org.springframework.beans.factory.annotation.Autowired;
2128
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
2229
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
2330
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
24-
import org.springframework.data.relational.core.query.Criteria;
25-
import org.springframework.data.relational.core.query.CriteriaDefinition;
26-
import org.springframework.data.relational.core.query.Query;
27-
28-
import java.util.List;
29-
30-
import static org.assertj.core.api.Assertions.*;
3131

3232
/**
3333
* Run tests demonstrating the use of Single Query Loading. You'll have to observe the executed queries.
@@ -38,52 +38,26 @@
3838
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
3939
class SingleQueryLoadingApplicationTests {
4040

41-
@Autowired
42-
PetOwnerRepository petOwners;
43-
@Autowired
44-
JdbcAggregateTemplate template;
41+
@Autowired PetOwnerRepository petOwners;
42+
@Autowired JdbcAggregateTemplate template;
43+
4544
private PetOwner emil;
4645
private PetOwner marry;
4746

4847
@BeforeEach
4948
void setup() {
49+
5050
petOwners.deleteAll();
51-
emil = petOwners.save(new PetOwner("Emil",
52-
List.of(
53-
new Cat("Edgar"),
54-
new Cat("Einstein"),
55-
new Cat("Elliot"),
56-
new Cat("Elton"),
57-
new Cat("Evan")
58-
),
59-
List.of(
60-
new Dog("Eric"),
61-
new Dog("Eddie"),
62-
new Dog("Eke"),
63-
new Dog("Echo")
64-
),
65-
List.of(
66-
new Fish("Floaty")
67-
)
6851

52+
emil = petOwners.save(new PetOwner("Emil", //
53+
List.of(new Cat("Edgar"), new Cat("Einstein"), new Cat("Elliot"), new Cat("Elton"), new Cat("Evan")), //
54+
List.of(new Dog("Eric"), new Dog("Eddie"), new Dog("Eke"), new Dog("Echo")), //
55+
List.of(new Fish("Floaty Mc Floatface")) //
6956
));
7057

71-
marry = petOwners.save(new PetOwner("Marry",
72-
List.of(
73-
new Cat("Mars"),
74-
new Cat("Maverick"),
75-
new Cat("Max")
76-
),
77-
List.of(
78-
new Dog("Molly"),
79-
new Dog("Murphy"),
80-
new Dog("Madison"),
81-
new Dog("Macie")
82-
),
83-
List.of(
84-
new Fish("Mahi Mahi"),
85-
new Fish("Mr. Limpet")
86-
)
58+
marry = petOwners.save(new PetOwner("Marry", List.of(new Cat("Mars"), new Cat("Maverick"), new Cat("Max")), //
59+
List.of(new Dog("Molly"), new Dog("Murphy"), new Dog("Madison"), new Dog("Macie")), //
60+
List.of(new Fish("Mahi Mahi"), new Fish("Mr. Limpet")) //
8761
));
8862
}
8963

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

9569
assertThat(emilReloaded).isEqualTo(emil);
96-
9770
}
9871

9972
@Test
100-
void loadByName() {
73+
void loadByNameUsingTemplate() {
74+
75+
List<PetOwner> marries = (List<PetOwner>) template.findAll(query(where("name").is("Marry")), PetOwner.class);
10176

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

0 commit comments

Comments
 (0)