#395 - Correcting handling of immutable properties.
If properties are final they need to have a matching argument in the persistence constructor or a matching "Wither". Otherwise trying to set them results in an exception.
This commit is contained in:
committed by
Jens Schauder
parent
b155e9273d
commit
f6432092fa
@@ -15,7 +15,10 @@
|
||||
*/
|
||||
package example.springdata.jdbc.basics.aggregate;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Wither;
|
||||
|
||||
import java.time.Period;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
@@ -34,6 +37,7 @@ import org.springframework.data.annotation.Transient;
|
||||
*/
|
||||
@Data
|
||||
@AccessType(Type.PROPERTY)
|
||||
@AllArgsConstructor(access = AccessLevel.PACKAGE)
|
||||
public class LegoSet {
|
||||
|
||||
private @Id int id;
|
||||
@@ -47,8 +51,11 @@ public class LegoSet {
|
||||
private Manual manual;
|
||||
|
||||
// You can build multiple models from one LegoSet
|
||||
@AccessType(Type.FIELD)
|
||||
private final Map<String, Model> models = new HashMap<>();
|
||||
private final @AccessType(Type.FIELD) @Wither(AccessLevel.PACKAGE) Map<String, Model> models;
|
||||
|
||||
LegoSet() {
|
||||
this.models = new HashMap<>();
|
||||
}
|
||||
|
||||
// conversion for custom types currently has to be done through getters/setter + marking the underlying property with
|
||||
// @Transient.
|
||||
|
||||
@@ -15,7 +15,9 @@
|
||||
*/
|
||||
package example.springdata.jdbc.basics.aggregate;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Value;
|
||||
import lombok.experimental.Wither;
|
||||
|
||||
/**
|
||||
* One of potentially multiple models that can be build from a single {@link LegoSet}. No getters or setters needed.
|
||||
@@ -23,6 +25,7 @@ import lombok.Value;
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@Value
|
||||
@Wither(AccessLevel.PACKAGE)
|
||||
public class Model {
|
||||
String name, description;
|
||||
}
|
||||
|
||||
@@ -15,12 +15,15 @@
|
||||
*/
|
||||
package example.springdata.jdbc.basics.aggregate;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Value;
|
||||
import lombok.experimental.Wither;
|
||||
|
||||
/**
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@Value
|
||||
@Wither(AccessLevel.PACKAGE)
|
||||
public class ModelReport {
|
||||
String modelName, description, setName;
|
||||
}
|
||||
|
||||
@@ -17,12 +17,16 @@ package example.springdata.jdbc.basics.simpleentity;
|
||||
|
||||
import example.springdata.jdbc.basics.aggregate.AgeGroup;
|
||||
import example.springdata.jdbc.basics.aggregate.LegoSet;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.Setter;
|
||||
import lombok.experimental.Wither;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.PersistenceConstructor;
|
||||
|
||||
/**
|
||||
* Coarse classification for {@link LegoSet}s, like "Car", "Plane", "Building" and so on.
|
||||
@@ -30,11 +34,12 @@ import org.springframework.data.annotation.Id;
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
|
||||
public class Category {
|
||||
|
||||
private final @Id Long id;
|
||||
private final @Id @Wither Long id;
|
||||
private String name, description;
|
||||
private LocalDateTime created = LocalDateTime.now();
|
||||
private LocalDateTime created;
|
||||
private @Setter long inserted;
|
||||
private AgeGroup ageGroup;
|
||||
|
||||
@@ -44,6 +49,7 @@ public class Category {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.ageGroup = ageGroup;
|
||||
this.created = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public void timeStamp() {
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
package example.springdata.jdbc.basics.simpleentity;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import example.springdata.jdbc.basics.Output;
|
||||
@@ -44,12 +43,10 @@ public class SimpleEntityTests {
|
||||
public void exerciseRepositoryForSimpleEntity() {
|
||||
|
||||
// create some categories
|
||||
Category cars = new Category("Cars", "Anything that has approximately 4 wheels", AgeGroup._3to8);
|
||||
|
||||
Category buildings = new Category("Buildings", null, AgeGroup._12andOlder);
|
||||
Category cars = repository.save(new Category("Cars", "Anything that has approximately 4 wheels", AgeGroup._3to8));
|
||||
Category buildings = repository.save(new Category("Buildings", null, AgeGroup._12andOlder));
|
||||
|
||||
// save categories
|
||||
repository.saveAll(asList(cars, buildings));
|
||||
Output.list(repository.findAll(), "`Cars` and `Buildings` got saved");
|
||||
|
||||
assertThat(cars.getId()).isNotNull();
|
||||
|
||||
@@ -30,8 +30,7 @@ import org.springframework.data.annotation.Id;
|
||||
@Data
|
||||
public class LegoSet {
|
||||
|
||||
// You can build multiple models from one LegoSet
|
||||
private final Map<String, Model> models = new HashMap<>();
|
||||
private Map<String, Model> models = new HashMap<>();
|
||||
|
||||
private @Id Integer id;
|
||||
private String name;
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<spring-data-releasetrain.version>Lovelace-RC2</spring-data-releasetrain.version>
|
||||
<spring-data-releasetrain.version>Lovelace-BUILD-SNAPSHOT</spring-data-releasetrain.version>
|
||||
</properties>
|
||||
|
||||
<profiles>
|
||||
@@ -34,7 +34,7 @@
|
||||
<profile>
|
||||
<id>spring-data-next</id>
|
||||
<properties>
|
||||
<spring-data-releasetrain.version>Lovelace-RC2</spring-data-releasetrain.version>
|
||||
<spring-data-releasetrain.version>Lovelace-BUILD-SNAPSHOT</spring-data-releasetrain.version>
|
||||
</properties>
|
||||
</profile>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user