Skip to content

Commit f643209

Browse files
odrotbohmschauder
authored andcommitted
#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.
1 parent b155e92 commit f643209

File tree

7 files changed

+28
-13
lines changed

7 files changed

+28
-13
lines changed

jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/LegoSet.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616
package example.springdata.jdbc.basics.aggregate;
1717

18+
import lombok.AccessLevel;
19+
import lombok.AllArgsConstructor;
1820
import lombok.Data;
21+
import lombok.experimental.Wither;
1922

2023
import java.time.Period;
2124
import java.time.temporal.ChronoUnit;
@@ -34,6 +37,7 @@
3437
*/
3538
@Data
3639
@AccessType(Type.PROPERTY)
40+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
3741
public class LegoSet {
3842

3943
private @Id int id;
@@ -47,8 +51,11 @@ public class LegoSet {
4751
private Manual manual;
4852

4953
// You can build multiple models from one LegoSet
50-
@AccessType(Type.FIELD)
51-
private final Map<String, Model> models = new HashMap<>();
54+
private final @AccessType(Type.FIELD) @Wither(AccessLevel.PACKAGE) Map<String, Model> models;
55+
56+
LegoSet() {
57+
this.models = new HashMap<>();
58+
}
5259

5360
// conversion for custom types currently has to be done through getters/setter + marking the underlying property with
5461
// @Transient.

jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/Model.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
*/
1616
package example.springdata.jdbc.basics.aggregate;
1717

18+
import lombok.AccessLevel;
1819
import lombok.Value;
20+
import lombok.experimental.Wither;
1921

2022
/**
2123
* One of potentially multiple models that can be build from a single {@link LegoSet}. No getters or setters needed.
2224
*
2325
* @author Jens Schauder
2426
*/
2527
@Value
28+
@Wither(AccessLevel.PACKAGE)
2629
public class Model {
2730
String name, description;
2831
}

jdbc/basics/src/main/java/example/springdata/jdbc/basics/aggregate/ModelReport.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
*/
1616
package example.springdata.jdbc.basics.aggregate;
1717

18+
import lombok.AccessLevel;
1819
import lombok.Value;
20+
import lombok.experimental.Wither;
1921

2022
/**
2123
* @author Jens Schauder
2224
*/
2325
@Value
26+
@Wither(AccessLevel.PACKAGE)
2427
public class ModelReport {
2528
String modelName, description, setName;
2629
}

jdbc/basics/src/main/java/example/springdata/jdbc/basics/simpleentity/Category.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,29 @@
1717

1818
import example.springdata.jdbc.basics.aggregate.AgeGroup;
1919
import example.springdata.jdbc.basics.aggregate.LegoSet;
20+
import lombok.AccessLevel;
21+
import lombok.AllArgsConstructor;
2022
import lombok.Data;
2123
import lombok.Setter;
24+
import lombok.experimental.Wither;
2225

2326
import java.time.LocalDateTime;
2427

2528
import org.springframework.data.annotation.Id;
29+
import org.springframework.data.annotation.PersistenceConstructor;
2630

2731
/**
2832
* Coarse classification for {@link LegoSet}s, like "Car", "Plane", "Building" and so on.
2933
*
3034
* @author Jens Schauder
3135
*/
3236
@Data
37+
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
3338
public class Category {
3439

35-
private final @Id Long id;
40+
private final @Id @Wither Long id;
3641
private String name, description;
37-
private LocalDateTime created = LocalDateTime.now();
42+
private LocalDateTime created;
3843
private @Setter long inserted;
3944
private AgeGroup ageGroup;
4045

@@ -44,6 +49,7 @@ public Category(String name, String description, AgeGroup ageGroup) {
4449
this.name = name;
4550
this.description = description;
4651
this.ageGroup = ageGroup;
52+
this.created = LocalDateTime.now();
4753
}
4854

4955
public void timeStamp() {

jdbc/basics/src/test/java/example/springdata/jdbc/basics/simpleentity/SimpleEntityTests.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package example.springdata.jdbc.basics.simpleentity;
1717

18-
import static java.util.Arrays.*;
1918
import static org.assertj.core.api.Assertions.*;
2019

2120
import example.springdata.jdbc.basics.Output;
@@ -44,12 +43,10 @@ public class SimpleEntityTests {
4443
public void exerciseRepositoryForSimpleEntity() {
4544

4645
// create some categories
47-
Category cars = new Category("Cars", "Anything that has approximately 4 wheels", AgeGroup._3to8);
48-
49-
Category buildings = new Category("Buildings", null, AgeGroup._12andOlder);
46+
Category cars = repository.save(new Category("Cars", "Anything that has approximately 4 wheels", AgeGroup._3to8));
47+
Category buildings = repository.save(new Category("Buildings", null, AgeGroup._12andOlder));
5048

5149
// save categories
52-
repository.saveAll(asList(cars, buildings));
5350
Output.list(repository.findAll(), "`Cars` and `Buildings` got saved");
5451

5552
assertThat(cars.getId()).isNotNull();

jdbc/mybatis/src/main/java/example/springdata/jdbc/mybatis/LegoSet.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
@Data
3131
public class LegoSet {
3232

33-
// You can build multiple models from one LegoSet
34-
private final Map<String, Model> models = new HashMap<>();
33+
private Map<String, Model> models = new HashMap<>();
3534

3635
private @Id Integer id;
3736
private String name;

jdbc/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</modules>
2525

2626
<properties>
27-
<spring-data-releasetrain.version>Lovelace-RC2</spring-data-releasetrain.version>
27+
<spring-data-releasetrain.version>Lovelace-BUILD-SNAPSHOT</spring-data-releasetrain.version>
2828
</properties>
2929

3030
<profiles>
@@ -34,7 +34,7 @@
3434
<profile>
3535
<id>spring-data-next</id>
3636
<properties>
37-
<spring-data-releasetrain.version>Lovelace-RC2</spring-data-releasetrain.version>
37+
<spring-data-releasetrain.version>Lovelace-BUILD-SNAPSHOT</spring-data-releasetrain.version>
3838
</properties>
3939
</profile>
4040

0 commit comments

Comments
 (0)