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

View File

@@ -66,4 +66,4 @@
</plugins>
</build>
</project>
</project>

View File

@@ -15,12 +15,8 @@
*/
package example.springdata.jdbc.jmolecules.customer;
import lombok.Value;
import org.jmolecules.ddd.annotation.ValueObject;
@Value
@ValueObject
public class Address {
private final String street, city, zipCode;
public record Address(String street, String city, String zipCode) {
}

View File

@@ -48,7 +48,7 @@ public class Customer implements AggregateRoot<Customer, CustomerId> {
Assert.notNull(address, "Address must not be null!");
this.id = CustomerId.of(UUID.randomUUID().toString());
this.id = new CustomerId(UUID.randomUUID().toString());
this.firstname = firstname;
this.lastname = lastname;
@@ -57,8 +57,6 @@ public class Customer implements AggregateRoot<Customer, CustomerId> {
this.addresses.add(address);
}
@Value(staticConstructor = "of")
public static class CustomerId implements Identifier {
private final String id;
public record CustomerId(String id) implements Identifier {
}
}

View File

@@ -15,12 +15,8 @@
*/
package example.springdata.jdbc.jmolecules.order;
import lombok.Value;
import org.jmolecules.ddd.annotation.ValueObject;
@Value
@ValueObject
public class LineItem {
String description;
public record LineItem(String description) {
}

View File

@@ -20,7 +20,6 @@ import example.springdata.jdbc.jmolecules.customer.Customer.CustomerId;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.Value;
import java.util.ArrayList;
import java.util.List;
@@ -47,7 +46,7 @@ public class Order implements AggregateRoot<Order, Order.OrderId> {
public Order(Customer customer) {
this.id = OrderId.of(UUID.randomUUID());
this.id = new OrderId(UUID.randomUUID());
this.customer = Association.forAggregate(customer);
this.lineItems = new ArrayList<>();
}
@@ -61,13 +60,10 @@ public class Order implements AggregateRoot<Order, Order.OrderId> {
return this;
}
@Value(staticConstructor = "of")
public static class OrderId implements Identifier {
private final UUID orderId;
public record OrderId(UUID orderId) implements Identifier {
public static OrderId create() {
return OrderId.of(UUID.randomUUID());
return new OrderId(UUID.randomUUID());
}
}
}

View File

@@ -28,17 +28,12 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
/**
* @author Oliver Drotbohm
*/
@SpringBootTest
@RequiredArgsConstructor
class ApplicationIntegrationTests {
private final ConfigurableApplicationContext context;
record ApplicationIntegrationTests(ConfigurableApplicationContext context) {
@Test
void exposesAssociationInMetamodel() {

View File

@@ -11,13 +11,7 @@ import org.jooq.DSLContext;
*
* @author Florian Lüdiger
*/
public class JooqRepositoryImpl implements JooqRepository {
private final DSLContext dslContext;
public JooqRepositoryImpl(DSLContext dslContext) {
this.dslContext = dslContext;
}
public record JooqRepositoryImpl(DSLContext dslContext) implements JooqRepository {
public List<Category> getCategoriesWithAgeGroup(AgeGroup ageGroup) {
return this.dslContext.select().from(CATEGORY).where(CATEGORY.AGE_GROUP.equal(ageGroup.name()))

View File

@@ -53,7 +53,7 @@ class SimpleEntityTests {
var saved = repository.saveAll(asList(cars, buildings));
Output.list(repository.findAll(), "`Cars` and `Buildings` got saved");
assertThat(saved).extracting(c -> c.getId()).isNotNull();
assertThat(saved).extracting(Category::getId).isNotNull();
// update one
buildings.setDescription("Famous and impressive buildings incl. the 'bike shed'.");

View File

@@ -28,7 +28,7 @@ import org.springframework.boot.test.context.SpringBootTest;
* Demonstrates queries can be mapped using MyBatis.
*
* @author Jens Schauder
* @author Divya Srivastava
* @author Divya Srivastava
*/
@SpringBootTest(classes = MyBatisConfiguration.class)
@AutoConfigureJdbc