Skip to content

Commit c47cd08

Browse files
schaudergregturn
authored andcommitted
DATAJDBC-276 - Id are no longer required for elements of Lists.
1 parent 9fb5aad commit c47cd08

File tree

7 files changed

+69
-20
lines changed

7 files changed

+69
-20
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AggregateTemplateIntegrationTests.java

+36-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
import org.springframework.test.context.junit4.rules.SpringMethodRule;
4040
import org.springframework.transaction.annotation.Transactional;
4141

42+
import java.util.ArrayList;
43+
import java.util.List;
44+
4245
/**
4346
* Integration tests for {@link JdbcAggregateTemplate}.
4447
*
@@ -217,7 +220,7 @@ public void oneToOneChildWithoutId() {
217220
OneToOneParent parent = new OneToOneParent();
218221

219222
parent.content = "parent content";
220-
parent.child = new OneToOneChildNoId();
223+
parent.child = new ChildNoId();
221224
parent.child.content = "child content";
222225

223226
template.save(parent);
@@ -248,7 +251,7 @@ public void oneToOneNullAttributes() {
248251
OneToOneParent parent = new OneToOneParent();
249252

250253
parent.content = "parent content";
251-
parent.child = new OneToOneChildNoId();
254+
parent.child = new ChildNoId();
252255

253256
template.save(parent);
254257

@@ -289,6 +292,23 @@ public void saveAndLoadAnEntityWithSecondaryReferenceNotNull() {
289292

290293
softly.assertAll();
291294
}
295+
@Test // DATAJDBC-276
296+
public void saveAndLoadAnEntityWithListOfElementsWithoutId() {
297+
298+
ListParent entity = new ListParent();
299+
entity.name = "name";
300+
301+
ElementNoId element = new ElementNoId();
302+
element.content = "content";
303+
304+
entity.content.add(element);
305+
306+
template.save(entity);
307+
308+
ListParent reloaded = template.findById(entity.id, ListParent.class);
309+
310+
assertThat(reloaded.content).extracting(e -> e.content).containsExactly("content");
311+
}
292312

293313
private static LegoSet createLegoSet() {
294314

@@ -326,13 +346,25 @@ static class OneToOneParent {
326346
@Id private Long id;
327347
private String content;
328348

329-
private OneToOneChildNoId child;
349+
private ChildNoId child;
330350
}
331351

332-
static class OneToOneChildNoId {
352+
static class ChildNoId {
333353
private String content;
334354
}
335355

356+
static class ListParent {
357+
358+
@Id private Long id;
359+
String name;
360+
List<ElementNoId> content = new ArrayList<>();
361+
}
362+
363+
static class ElementNoId {
364+
private String content;
365+
}
366+
367+
336368
@Configuration
337369
@Import(TestConfiguration.class)
338370
static class Config {

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/SqlGeneratorUnitTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void deleteMapByPath() {
139139
@Test // DATAJDBC-131
140140
public void findAllByProperty() {
141141

142-
// this would get called when DummyEntity is the element type of a Set
142+
// this would get called when ListParent is the element type of a Set
143143
String sql = sqlGenerator.getFindAllByProperty("back-ref", null, false);
144144

145145
assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, " //
@@ -152,7 +152,7 @@ public void findAllByProperty() {
152152
@Test // DATAJDBC-131
153153
public void findAllByPropertyWithKey() {
154154

155-
// this would get called when DummyEntity is th element type of a Map
155+
// this would get called when ListParent is th element type of a Map
156156
String sql = sqlGenerator.getFindAllByProperty("back-ref", "key-column", false);
157157

158158
assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, " //
@@ -171,7 +171,7 @@ public void findAllByPropertyOrderedWithoutKey() {
171171
@Test // DATAJDBC-131
172172
public void findAllByPropertyWithKeyOrdered() {
173173

174-
// this would get called when DummyEntity is th element type of a Map
174+
// this would get called when ListParent is th element type of a Map
175175
String sql = sqlGenerator.getFindAllByProperty("back-ref", "key-column", true);
176176

177177
assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, " //

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-hsql.sql

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
55
REFERENCES LEGO_SET(id);
66

77
CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, content VARCHAR(30));
8-
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
8+
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
9+
10+
CREATE TABLE LIST_PARENT ( id BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY, NAME VARCHAR(100));
11+
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT BIGINT);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mariadb.sql

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
55
REFERENCES LEGO_SET(id);
66

77
CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, content VARCHAR(30));
8-
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
8+
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
9+
10+
CREATE TABLE LIST_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(100));
11+
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT BIGINT);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-mysql.sql

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
55
REFERENCES LEGO_SET(id);
66

77
CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, content VARCHAR(30));
8-
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
8+
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
9+
10+
CREATE TABLE LIST_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(100));
11+
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT BIGINT);

spring-data-jdbc/src/test/resources/org.springframework.data.jdbc.core/AggregateTemplateIntegrationTests-postgres.sql

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
88
REFERENCES LEGO_SET(id);
99

1010
CREATE TABLE ONE_TO_ONE_PARENT ( id SERIAL PRIMARY KEY, content VARCHAR(30));
11-
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
11+
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
12+
13+
CREATE TABLE LIST_PARENT ( id SERIAL PRIMARY KEY, NAME VARCHAR(100));
14+
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT INTEGER);

spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/AggregateChange.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,6 @@ static void setId(RelationalMappingContext context, RelationalConverter converte
107107

108108
PersistentPropertyPath<RelationalPersistentProperty> propertyPathToEntity = action.getPropertyPath();
109109

110-
RelationalPersistentProperty requiredIdProperty = context
111-
.getRequiredPersistentEntity(propertyPathToEntity.getRequiredLeafProperty().getActualType())
112-
.getRequiredIdProperty();
113-
114-
PersistentPropertyPath<RelationalPersistentProperty> pathToId = context.getPersistentPropertyPath(
115-
propertyPathToEntity.toDotPath() + '.' + requiredIdProperty.getName(),
116-
propertyPathToEntity.getBaseProperty().getOwner().getType());
117-
118110
RelationalPersistentProperty leafProperty = propertyPathToEntity.getRequiredLeafProperty();
119111

120112
Object currentPropertyValue = propertyAccessor.getProperty(propertyPathToEntity);
@@ -140,6 +132,15 @@ static void setId(RelationalMappingContext context, RelationalConverter converte
140132
throw new IllegalStateException("Can't handle " + currentPropertyValue);
141133
}
142134
} else {
135+
136+
RelationalPersistentProperty requiredIdProperty = context
137+
.getRequiredPersistentEntity(propertyPathToEntity.getRequiredLeafProperty().getActualType())
138+
.getRequiredIdProperty();
139+
140+
PersistentPropertyPath<RelationalPersistentProperty> pathToId = context.getPersistentPropertyPath(
141+
propertyPathToEntity.toDotPath() + '.' + requiredIdProperty.getName(),
142+
propertyPathToEntity.getBaseProperty().getOwner().getType());
143+
143144
propertyAccessor.setProperty(pathToId, generatedId);
144145
}
145146
}
@@ -191,7 +192,11 @@ private static <T> PersistentPropertyAccessor<T> setId(RelationalConverter conve
191192
PersistentPropertyAccessor<T> intermediateAccessor = converter.getPropertyAccessor(persistentEntity,
192193
(T) originalElement);
193194

194-
intermediateAccessor.setProperty(persistentEntity.getRequiredIdProperty(), generatedId);
195+
RelationalPersistentProperty idProperty = persistentEntity.getIdProperty();
196+
if (idProperty != null) {
197+
intermediateAccessor.setProperty(idProperty, generatedId);
198+
}
199+
195200
return intermediateAccessor;
196201
}
197202

0 commit comments

Comments
 (0)