-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples to use Single Query Loading.
Query by Id works via the repository. Query derivation doesn't since it side steps the DataAccessStrategy. Closes #675
- Loading branch information
Showing
11 changed files
with
387 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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> | ||
|
||
<artifactId>singlequeryloading</artifactId> | ||
<properties> | ||
<maven.compiler.source>21</maven.compiler.source> | ||
<maven.compiler.target>21</maven.compiler.target> | ||
</properties> | ||
|
||
<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> | ||
|
||
<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> | ||
</project> |
24 changes: 24 additions & 0 deletions
24
jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/Cat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2023 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 | ||
* | ||
* https://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; | ||
|
||
/** | ||
* A simple entity for use in a collection of {@link PetOwner}. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
record Cat(String name) { | ||
} |
45 changes: 45 additions & 0 deletions
45
jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2023 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 | ||
* | ||
* https://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.boot.SpringBootConfiguration; | ||
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions; | ||
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; | ||
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; | ||
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories; | ||
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. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
@SpringBootConfiguration | ||
@EnableJdbcRepositories | ||
public class Config extends AbstractJdbcConfiguration { | ||
|
||
|
||
@Override | ||
public JdbcMappingContext jdbcMappingContext(Optional<NamingStrategy> namingStrategy, JdbcCustomConversions customConversions, RelationalManagedTypes jdbcManagedTypes) { | ||
|
||
JdbcMappingContext jdbcMappingContext = super.jdbcMappingContext(namingStrategy, customConversions, jdbcManagedTypes); | ||
jdbcMappingContext.setSingleQueryLoadingEnabled(true); | ||
return jdbcMappingContext; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/Dog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2023 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 | ||
* | ||
* https://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; | ||
|
||
/** | ||
* A simple entity for use in a collection of {@link PetOwner}. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
record Dog(String name) { | ||
} |
24 changes: 24 additions & 0 deletions
24
jdbc/singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/Fish.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2023 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 | ||
* | ||
* https://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; | ||
|
||
/** | ||
* A simple entity for use in a collection of {@link PetOwner}. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
record Fish(String name) { | ||
} |
61 changes: 61 additions & 0 deletions
61
...singlequeryloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2023 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 | ||
* | ||
* https://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; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* An aggregate with mutliple collections. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
class PetOwner { | ||
|
||
@Id | ||
Long Id; | ||
|
||
String name; | ||
|
||
List<Dog> dogs = new ArrayList<>(); | ||
|
||
List<Cat> cats = new ArrayList<>(); | ||
|
||
List<Fish> fish = new ArrayList<>(); | ||
|
||
public PetOwner(String name, List<Cat> cats, List<Dog> dogs, List<Fish> fish) { | ||
|
||
this.name = name; | ||
this.cats = cats; | ||
this.dogs = dogs; | ||
this.fish = fish; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
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); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(Id, name, dogs, cats, fish); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...yloading/src/main/java/example/springdata/jdbc/singlequeryloading/PetOwnerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2023 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 | ||
* | ||
* https://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; | ||
|
||
|
||
/** | ||
* Repository to access {@link PetOwner} instances. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
interface PetOwnerRepository extends ListCrudRepository<PetOwner, Long> { | ||
List<PetOwner> findByName(String marry); | ||
} |
3 changes: 3 additions & 0 deletions
3
jdbc/singlequeryloading/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
spring.datasource.url=jdbc:tc:postgresql:16.0:///test | ||
logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG | ||
spring.sql.init.mode=always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
CREATE TABLE PET_OWNER | ||
( | ||
ID SERIAL PRIMARY KEY, | ||
NAME VARCHAR(255) | ||
); | ||
|
||
CREATE TABLE CAT | ||
( | ||
PET_OWNER INT, | ||
PET_OWNER_KEY INT, | ||
NAME VARCHAR(255) | ||
); | ||
|
||
CREATE TABLE DOG | ||
( | ||
PET_OWNER INT, | ||
PET_OWNER_KEY INT, | ||
NAME VARCHAR(255) | ||
); | ||
|
||
CREATE TABLE FISH | ||
( | ||
PET_OWNER INT, | ||
PET_OWNER_KEY INT, | ||
NAME VARCHAR(255) | ||
); | ||
|
Oops, something went wrong.