#381 - Adapt examples to immutable object support.

Provide mutable property accessors to allow object mutation.
This commit is contained in:
Mark Paluch
2018-07-26 11:07:42 +02:00
parent e2d2bdf6bc
commit 1c56cf8fba
5 changed files with 29 additions and 14 deletions

View File

@@ -15,13 +15,13 @@
*/
package example.springdata.mongodb.aggregation;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.mapping.Document;
@@ -31,16 +31,17 @@ import org.springframework.data.mongodb.core.mapping.Document;
*
* @author Thomas Darimont
* @author Oliver Gierke
* @author Mark Paluch
*/
@Data
@RequiredArgsConstructor(onConstructor = @__(@PersistenceConstructor))
@AllArgsConstructor(onConstructor = @__(@PersistenceConstructor))
@Document
public class Order {
private final String id;
private final String customerId;
private final Date orderDate;
private final List<LineItem> items;
private String id;
private String customerId;
private Date orderDate;
private List<LineItem> items;
/**
* Creates a new {@link Order} for the given customer id and order date.
@@ -49,7 +50,7 @@ public class Order {
* @param orderDate
*/
public Order(String customerId, Date orderDate) {
this(null, customerId, orderDate, new ArrayList<LineItem>());
this(null, customerId, orderDate, new ArrayList<>());
}
/**

View File

@@ -15,7 +15,7 @@
*/
package example.springdata.mongodb.projections;
import lombok.Value;
import lombok.Data;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
@@ -23,11 +23,17 @@ import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author Oliver Gierke
* @author Mark Paluch
*/
@Value
@Data
@Document
class Customer {
@Id ObjectId id = new ObjectId();
String firstname, lastname;
public Customer(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}

View File

@@ -18,6 +18,7 @@ package example.springdata.rest.stores;
import static org.springframework.data.mongodb.core.index.GeoSpatialIndexType.*;
import lombok.Value;
import lombok.experimental.Wither;
import java.util.UUID;
@@ -30,12 +31,14 @@ import org.springframework.data.mongodb.core.mapping.Document;
* Entity to represent a {@link Store}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
@Value
@Wither
@Document
public class Store {
@Id UUID id = UUID.randomUUID();
@Id UUID id;
String name;
Address address;

View File

@@ -21,6 +21,7 @@ import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.UUID;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.file.FlatFileItemReader;
@@ -37,6 +38,7 @@ import org.springframework.stereotype.Component;
* Component initializing a hand full of Starbucks stores and persisting them through a {@link StoreRepository}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
@Slf4j
@Component
@@ -84,7 +86,7 @@ public class StoreInitializer {
Address address = new Address(fields.readString("Street Address"), fields.readString("City"),
fields.readString("Zip"), location);
return new Store(fields.readString("Name"), address);
return new Store(UUID.randomUUID(), fields.readString("Name"), address);
});
lineMapper.setLineTokenizer(tokenizer);

View File

@@ -20,6 +20,8 @@ import static org.junit.Assert.*;
import example.springdata.rest.stores.Store.Address;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -37,6 +39,7 @@ import org.springframework.test.context.junit4.SpringRunner;
* Integration tests for {@link StoreRepository}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@@ -54,7 +57,7 @@ public class StoreRepositoryIntegrationTests {
public void findsStoresByLocation() {
Point location = new Point(-73.995146, 40.740337);
Store store = new Store("Foo", new Address("street", "city", "zip", location));
Store store = new Store(UUID.randomUUID(), "Foo", new Address("street", "city", "zip", location));
store = repository.save(store);