@@ -15,13 +15,13 @@
|
||||
*/
|
||||
package example.springdata.jdbc.basics.aggregate;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jdbc.repository.query.Modifying;
|
||||
import org.springframework.data.jdbc.repository.query.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A repository for {@link LegoSet}.
|
||||
*
|
||||
@@ -29,11 +29,13 @@ import java.util.List;
|
||||
*/
|
||||
interface LegoSetRepository extends CrudRepository<LegoSet, Integer> {
|
||||
|
||||
@Query("SELECT m.name model_name, m.description, l.name set_name" +
|
||||
" FROM model m" +
|
||||
" JOIN lego_set l" +
|
||||
" ON m.lego_set = l.id" +
|
||||
" WHERE :age BETWEEN l.min_age and l.max_age")
|
||||
@Query("""
|
||||
SELECT m.name model_name, m.description, l.name set_name
|
||||
FROM model m
|
||||
JOIN lego_set l
|
||||
ON m.lego_set = l.id
|
||||
WHERE :age BETWEEN l.min_age and l.max_age
|
||||
""")
|
||||
List<ModelReport> reportModelForAge(@Param("age") int age);
|
||||
|
||||
/**
|
||||
@@ -41,9 +43,11 @@ interface LegoSetRepository extends CrudRepository<LegoSet, Integer> {
|
||||
* @param name
|
||||
* @return
|
||||
*/
|
||||
@Query("select a.*, b.handbuch_id as manual_handbuch_id, b.author as manual_author, b.text as manual_text from lego_set a " +
|
||||
"join handbuch b on a.id = b.handbuch_id " +
|
||||
"where a.name = :name")
|
||||
@Query("""
|
||||
select a.*, b.handbuch_id as manual_handbuch_id, b.author as manual_author, b.text as manual_text from lego_set a
|
||||
join handbuch b on a.id = b.handbuch_id
|
||||
where a.name = :name
|
||||
""")
|
||||
List<LegoSet> findByName(@Param("name") String name);
|
||||
|
||||
@Modifying
|
||||
|
||||
@@ -24,8 +24,5 @@ import lombok.With;
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@Value
|
||||
@With(AccessLevel.PACKAGE)
|
||||
public class Model {
|
||||
String name, description;
|
||||
public record Model(String name, String description) {
|
||||
}
|
||||
|
||||
@@ -22,8 +22,5 @@ import lombok.With;
|
||||
/**
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@Value
|
||||
@With(AccessLevel.PACKAGE)
|
||||
public class ModelReport {
|
||||
String modelName, description, setName;
|
||||
public record ModelReport(String modelName, String description, String setName) {
|
||||
}
|
||||
|
||||
@@ -103,13 +103,13 @@ class AggregateTests {
|
||||
Output.list(report, "Model Report");
|
||||
|
||||
assertThat(report).hasSize(7)
|
||||
.allMatch(m -> m.getDescription() != null && m.getModelName() != null && m.getSetName() != null);
|
||||
.allMatch(m -> m.description() != null && m.modelName() != null && m.setName() != null);
|
||||
|
||||
var updated = repository.lowerCaseMapKeys();
|
||||
// SUV, F1 Ferrari 2018 and Muck get updated
|
||||
assertThat(updated).isEqualTo(3);
|
||||
|
||||
final var legoSetsByName = repository.findByName(smallCarsSetName);
|
||||
var legoSetsByName = repository.findByName(smallCarsSetName);
|
||||
assertThat(legoSetsByName).hasSize(1);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user