Switch to records.

See #606.
This commit is contained in:
Mark Paluch
2021-04-28 09:25:28 +02:00
parent 32d118f046
commit 23dcae3a17
13 changed files with 30 additions and 57 deletions

View File

@@ -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

View File

@@ -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) {
}

View File

@@ -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) {
}

View File

@@ -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);
}