Skip to content

DATAJDBC-276 - Id are no longer required for elements of Lists. #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jdbc</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-276-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,6 @@ static void setId(RelationalMappingContext context, RelationalConverter converte

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

RelationalPersistentProperty requiredIdProperty = context
.getRequiredPersistentEntity(propertyPathToEntity.getRequiredLeafProperty().getActualType())
.getRequiredIdProperty();

PersistentPropertyPath<RelationalPersistentProperty> pathToId = context.getPersistentPropertyPath(
propertyPathToEntity.toDotPath() + '.' + requiredIdProperty.getName(),
propertyPathToEntity.getBaseProperty().getOwner().getType());

RelationalPersistentProperty leafProperty = propertyPathToEntity.getRequiredLeafProperty();

Object currentPropertyValue = propertyAccessor.getProperty(propertyPathToEntity);
Expand All @@ -140,6 +132,15 @@ static void setId(RelationalMappingContext context, RelationalConverter converte
throw new IllegalStateException("Can't handle " + currentPropertyValue);
}
} else {

RelationalPersistentProperty requiredIdProperty = context
.getRequiredPersistentEntity(propertyPathToEntity.getRequiredLeafProperty().getActualType())
.getRequiredIdProperty();

PersistentPropertyPath<RelationalPersistentProperty> pathToId = context.getPersistentPropertyPath(
propertyPathToEntity.toDotPath() + '.' + requiredIdProperty.getName(),
propertyPathToEntity.getBaseProperty().getOwner().getType());

propertyAccessor.setProperty(pathToId, generatedId);
}
}
Expand Down Expand Up @@ -191,7 +192,11 @@ private static <T> PersistentPropertyAccessor<T> setId(RelationalConverter conve
PersistentPropertyAccessor<T> intermediateAccessor = converter.getPropertyAccessor(persistentEntity,
(T) originalElement);

intermediateAccessor.setProperty(persistentEntity.getRequiredIdProperty(), generatedId);
RelationalPersistentProperty idProperty = persistentEntity.getIdProperty();
if (idProperty != null) {
intermediateAccessor.setProperty(idProperty, generatedId);
}

return intermediateAccessor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import org.springframework.test.context.junit4.rules.SpringMethodRule;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* Integration tests for {@link JdbcAggregateTemplate}.
*
Expand Down Expand Up @@ -217,7 +220,7 @@ public void oneToOneChildWithoutId() {
OneToOneParent parent = new OneToOneParent();

parent.content = "parent content";
parent.child = new OneToOneChildNoId();
parent.child = new ChildNoId();
parent.child.content = "child content";

template.save(parent);
Expand Down Expand Up @@ -248,7 +251,7 @@ public void oneToOneNullAttributes() {
OneToOneParent parent = new OneToOneParent();

parent.content = "parent content";
parent.child = new OneToOneChildNoId();
parent.child = new ChildNoId();

template.save(parent);

Expand Down Expand Up @@ -289,6 +292,23 @@ public void saveAndLoadAnEntityWithSecondaryReferenceNotNull() {

softly.assertAll();
}
@Test // DATAJDBC-276
public void saveAndLoadAnEntityWithListOfElementsWithoutId() {

ListParent entity = new ListParent();
entity.name = "name";

ElementNoId element = new ElementNoId();
element.content = "content";

entity.content.add(element);

template.save(entity);

ListParent reloaded = template.findById(entity.id, ListParent.class);

assertThat(reloaded.content).extracting(e -> e.content).containsExactly("content");
}

private static LegoSet createLegoSet() {

Expand Down Expand Up @@ -327,13 +347,25 @@ static class OneToOneParent {
@Id private Long id;
private String content;

private OneToOneChildNoId child;
private ChildNoId child;
}

static class OneToOneChildNoId {
static class ChildNoId {
private String content;
}

static class ListParent {

@Id private Long id;
String name;
List<ElementNoId> content = new ArrayList<>();
}

static class ElementNoId {
private String content;
}


@Configuration
@Import(TestConfiguration.class)
static class Config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void deleteMapByPath() {
@Test // DATAJDBC-131
public void findAllByProperty() {

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

assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, " //
Expand All @@ -151,7 +151,7 @@ public void findAllByProperty() {
@Test // DATAJDBC-131
public void findAllByPropertyWithKey() {

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

assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, " //
Expand All @@ -170,7 +170,7 @@ public void findAllByPropertyOrderedWithoutKey() {
@Test // DATAJDBC-131
public void findAllByPropertyWithKeyOrdered() {

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

assertThat(sql).isEqualTo("SELECT dummy_entity.x_id AS x_id, dummy_entity.x_name AS x_name, " //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
REFERENCES LEGO_SET(id);

CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, content VARCHAR(30));
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));

CREATE TABLE LIST_PARENT ( id BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY, NAME VARCHAR(100));
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT BIGINT);
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
REFERENCES LEGO_SET(id);

CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, content VARCHAR(30));
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));

CREATE TABLE LIST_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(100));
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT BIGINT);
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
REFERENCES LEGO_SET(id);

CREATE TABLE ONE_TO_ONE_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, content VARCHAR(30));
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));

CREATE TABLE LIST_PARENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(100));
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT BIGINT);
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ ALTER TABLE MANUAL ADD FOREIGN KEY (LEGO_SET)
REFERENCES LEGO_SET(id);

CREATE TABLE ONE_TO_ONE_PARENT ( id SERIAL PRIMARY KEY, content VARCHAR(30));
CREATE TABLE One_To_One_Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));
CREATE TABLE Child_No_Id (ONE_TO_ONE_PARENT INTEGER PRIMARY KEY, content VARCHAR(30));

CREATE TABLE LIST_PARENT ( id SERIAL PRIMARY KEY, NAME VARCHAR(100));
CREATE TABLE element_no_id ( content VARCHAR(100), LIST_PARENT_key BIGINT, LIST_PARENT INTEGER);