Skip to content

Commit

Permalink
Many to many colors use case
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Konstiak committed Mar 8, 2019
1 parent 5e80d9c commit 2a901bb
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2017-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.basics.aggregate;

import lombok.Builder;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

@Data
@Builder
public class Color {

private @Id Long id;
private String name;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package example.springdata.jdbc.basics.aggregate;

import lombok.Builder;
import org.springframework.data.relational.core.mapping.Table;

@Table("lego_set_color")
@Builder
public class ColorRef {
Long colorId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2017-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jdbc.basics.aggregate;

import org.springframework.data.repository.CrudRepository;

interface ColorRepository extends CrudRepository<Color, Integer> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.data.annotation.AccessType;
import org.springframework.data.annotation.AccessType.Type;
Expand All @@ -44,6 +46,8 @@ public class LegoSet {
private @Id int id;
private String name;
private @Transient Period minimumAge, maximumAge;
@Column(keyColumn = "lego_set_id")
private Set<ColorRef> colors = new HashSet<>();

/**
* Since Manuals are part of a {@link LegoSet} and only make sense inside a {@link LegoSet} it is considered part of
Expand Down Expand Up @@ -93,4 +97,8 @@ public void addModel(String name, String description) {
Model model = new Model(name, description);
models.put(name, model);
}

public void addColor(Color color) {
colors.add(ColorRef.builder().colorId(color.getId()).build());
}
}
5 changes: 5 additions & 0 deletions jdbc/basics/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ CREATE TABLE IF NOT EXISTS category (id INTEGER IDENTITY PRIMARY KEY, name VARCH
CREATE TABLE IF NOT EXISTS Lego_Set (id INTEGER, name VARCHAR(100), min_Age INTEGER, max_Age INTEGER);
CREATE TABLE IF NOT EXISTS Handbuch (handbuch_id INTEGER, author CHAR(100), text CLOB);
CREATE TABLE IF NOT EXISTS Model (name VARCHAR(100), description CLOB, lego_set INTEGER);

CREATE TABLE IF NOT EXISTS color (id INTEGER IDENTITY PRIMARY KEY, name VARCHAR(100));
CREATE TABLE IF NOT EXISTS lego_set_color (lego_set_id INTEGER, color_id INTEGER);


Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,43 @@
public class AggregateTests {

@Autowired LegoSetRepository repository;
@Autowired ColorRepository colorRepo;

@Test
public void exerciseSomewhatComplexEntity() {

Color blue = Color.builder().name("blue").build();
Color red = Color.builder().name("red").build();

colorRepo.save(blue);
colorRepo.save(red);

LegoSet smallCar = createLegoSet("Small Car 01", 5, 12);
smallCar.setManual(new Manual("Just put all the pieces together in the right order", "Jens Schauder"));
smallCar.addModel("suv", "SUV with sliding doors.");
smallCar.addModel("roadster", "Slick red roadster.");
smallCar.addColor(blue);
smallCar.addColor(red);

repository.save(smallCar);
Iterable<LegoSet> legoSets = repository.findAll();
Output.list(legoSets, "Original LegoSet");
checkLegoSets(legoSets, "Just put all the pieces together in the right order", 2);
checkLegoSets(legoSets, "Just put all the pieces together in the right order", 2, 2);

smallCar.getManual().setText("Just make it so it looks like a car.");
smallCar.addModel("pickup", "A pickup truck with some tools in the back.");

repository.save(smallCar);
legoSets = repository.findAll();
Output.list(legoSets, "Updated");
checkLegoSets(legoSets, "Just make it so it looks like a car.", 3);
checkLegoSets(legoSets, "Just make it so it looks like a car.", 3, 2);

smallCar.setManual(new Manual("One last attempt: Just build a car! Ok?", "Jens Schauder"));

repository.save(smallCar);
legoSets = repository.findAll();
Output.list(legoSets, "Manual replaced");
checkLegoSets(legoSets, "One last attempt: Just build a car! Ok?", 3);
checkLegoSets(legoSets, "One last attempt: Just build a car! Ok?", 3, 2);

}

Expand Down Expand Up @@ -124,12 +133,14 @@ private LegoSet createLegoSet(String name, int minimumAge, int maximumAge) {
return smallCar;
}

private void checkLegoSets(Iterable<LegoSet> legoSets, String manualText, int numberOfModels) {
private void checkLegoSets(Iterable<LegoSet> legoSets, String manualText, int numberOfModels, int numberOfColors) {

assertThat(legoSets) //
.extracting( //
ls -> ls.getManual().getText(), //
ls -> ls.getModels().size()) //
.containsExactly(new Tuple(manualText, numberOfModels));
ls -> ls.getModels().size(),
ls -> ls.getColors().size()
) //
.containsExactly(new Tuple(manualText, numberOfModels, numberOfColors));
}
}

0 comments on commit 2a901bb

Please sign in to comment.