@@ -1,41 +1,33 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>singlequeryloading</artifactId>
|
<artifactId>singlequeryloading</artifactId>
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>21</maven.compiler.source>
|
|
||||||
<maven.compiler.target>21</maven.compiler.target>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>org.springframework.data.examples</groupId>
|
<groupId>org.springframework.data.examples</groupId>
|
||||||
<artifactId>spring-data-jdbc-examples</artifactId>
|
<artifactId>spring-data-jdbc-examples</artifactId>
|
||||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||||
<relativePath>../pom.xml</relativePath>
|
<relativePath>../pom.xml</relativePath>
|
||||||
</parent>
|
</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>
|
<dependencies>
|
||||||
<description>Sample project demonstrating Single Query Loading with Spring Data JDBC</description>
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
<dependencies>
|
<artifactId>spring-boot-starter-data-jdbc</artifactId>
|
||||||
<dependency>
|
</dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<dependency>
|
||||||
<artifactId>spring-boot-starter-data-jdbc</artifactId>
|
<groupId>org.testcontainers</groupId>
|
||||||
</dependency>
|
<artifactId>postgresql</artifactId>
|
||||||
<dependency>
|
</dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<dependency>
|
||||||
<artifactId>spring-test</artifactId>
|
<groupId>org.postgresql</groupId>
|
||||||
</dependency>
|
<artifactId>postgresql</artifactId>
|
||||||
<dependency>
|
</dependency>
|
||||||
<groupId>org.testcontainers</groupId>
|
</dependencies>
|
||||||
<artifactId>postgresql</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.postgresql</groupId>
|
|
||||||
<artifactId>postgresql</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* 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
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
@@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package example.springdata.jdbc.singlequeryloading;
|
package example.springdata.jdbc.singlequeryloading;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.boot.SpringBootConfiguration;
|
import org.springframework.boot.SpringBootConfiguration;
|
||||||
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
|
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
|
||||||
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
|
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
|
||||||
@@ -23,8 +25,6 @@ import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
|
|||||||
import org.springframework.data.relational.RelationalManagedTypes;
|
import org.springframework.data.relational.RelationalManagedTypes;
|
||||||
import org.springframework.data.relational.core.mapping.NamingStrategy;
|
import org.springframework.data.relational.core.mapping.NamingStrategy;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spring application context configuration that enables Single Query Loading.
|
* Spring application context configuration that enables Single Query Loading.
|
||||||
*
|
*
|
||||||
@@ -34,11 +34,12 @@ import java.util.Optional;
|
|||||||
@EnableJdbcRepositories
|
@EnableJdbcRepositories
|
||||||
public class Config extends AbstractJdbcConfiguration {
|
public class Config extends AbstractJdbcConfiguration {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@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);
|
jdbcMappingContext.setSingleQueryLoadingEnabled(true);
|
||||||
return jdbcMappingContext;
|
return jdbcMappingContext;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,21 +5,21 @@
|
|||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* 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
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/package example.springdata.jdbc.singlequeryloading;
|
*/
|
||||||
|
package example.springdata.jdbc.singlequeryloading;
|
||||||
|
|
||||||
import org.springframework.data.annotation.Id;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import org.springframework.data.annotation.Id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An aggregate with mutliple collections.
|
* An aggregate with mutliple collections.
|
||||||
*
|
*
|
||||||
@@ -27,16 +27,15 @@ import java.util.Objects;
|
|||||||
*/
|
*/
|
||||||
class PetOwner {
|
class PetOwner {
|
||||||
|
|
||||||
@Id
|
@Id Long Id;
|
||||||
Long Id;
|
|
||||||
|
|
||||||
String name;
|
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) {
|
public PetOwner(String name, List<Cat> cats, List<Dog> dogs, List<Fish> fish) {
|
||||||
|
|
||||||
@@ -48,10 +47,13 @@ class PetOwner {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o)
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
return true;
|
||||||
|
if (o == null || getClass() != o.getClass())
|
||||||
|
return false;
|
||||||
PetOwner petOwner = (PetOwner) o;
|
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
|
@Override
|
||||||
|
|||||||
@@ -5,25 +5,21 @@
|
|||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* 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
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/package example.springdata.jdbc.singlequeryloading;
|
*/
|
||||||
|
package example.springdata.jdbc.singlequeryloading;
|
||||||
import org.springframework.data.repository.ListCrudRepository;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Repository to access {@link PetOwner} instances.
|
* Repository to access {@link PetOwner} instances.
|
||||||
*
|
*
|
||||||
* @author Jens Schauder
|
* @author Jens Schauder
|
||||||
*/
|
*/
|
||||||
interface PetOwnerRepository extends ListCrudRepository<PetOwner, Long> {
|
interface PetOwnerRepository extends CrudRepository<PetOwner, Long> {}
|
||||||
List<PetOwner> findByName(String marry);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* 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
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
@@ -15,19 +15,19 @@
|
|||||||
*/
|
*/
|
||||||
package example.springdata.jdbc.singlequeryloading;
|
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.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||||
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
|
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
|
||||||
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
|
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.
|
* Run tests demonstrating the use of Single Query Loading. You'll have to observe the executed queries.
|
||||||
@@ -38,52 +38,26 @@ import static org.assertj.core.api.Assertions.*;
|
|||||||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
|
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
|
||||||
class SingleQueryLoadingApplicationTests {
|
class SingleQueryLoadingApplicationTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired PetOwnerRepository petOwners;
|
||||||
PetOwnerRepository petOwners;
|
@Autowired JdbcAggregateTemplate template;
|
||||||
@Autowired
|
|
||||||
JdbcAggregateTemplate template;
|
|
||||||
private PetOwner emil;
|
private PetOwner emil;
|
||||||
private PetOwner marry;
|
private PetOwner marry;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
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")
|
|
||||||
)
|
|
||||||
|
|
||||||
|
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 Mc Floatface")) //
|
||||||
));
|
));
|
||||||
|
|
||||||
marry = petOwners.save(new PetOwner("Marry",
|
marry = petOwners.save(new PetOwner("Marry", List.of(new Cat("Mars"), new Cat("Maverick"), new Cat("Max")), //
|
||||||
List.of(
|
List.of(new Dog("Molly"), new Dog("Murphy"), new Dog("Madison"), new Dog("Macie")), //
|
||||||
new Cat("Mars"),
|
List.of(new Fish("Mahi Mahi"), new Fish("Mr. Limpet")) //
|
||||||
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 @@ class SingleQueryLoadingApplicationTests {
|
|||||||
PetOwner emilReloaded = petOwners.findById(emil.Id).orElseThrow();
|
PetOwner emilReloaded = petOwners.findById(emil.Id).orElseThrow();
|
||||||
|
|
||||||
assertThat(emilReloaded).isEqualTo(emil);
|
assertThat(emilReloaded).isEqualTo(emil);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@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);
|
assertThat(marries).containsExactly(marry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user