Skip to content

Commit eeafc04

Browse files
schaudergregturn
authored andcommitted
DATAJDBC-146 - Excluding id when provided as additional parameter.
If the id of an entity is provided as an additional parameter, i.e. the foreign key to an entity is the primary key, no id column is generated in the insert since it gets generated for the additional parameter.
1 parent 2fbb3b0 commit eeafc04

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

src/main/java/org/springframework/data/jdbc/core/DefaultDataAccessStrategy.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,15 @@ public <T> void insert(T instance, Class<T> domainType, Map<String, Object> addi
100100

101101
additionalParameters.forEach(parameterSource::addValue);
102102

103-
operations.update(sql(domainType).getInsert(idValue == null, additionalParameters.keySet()), parameterSource,
104-
holder);
103+
boolean idValueDoesNotComeFromEntity = //
104+
idValue == null //
105+
|| additionalParameters.containsKey(idProperty.getColumnName());
106+
107+
operations.update( //
108+
sql(domainType).getInsert(idValueDoesNotComeFromEntity, additionalParameters.keySet()), //
109+
parameterSource, //
110+
holder //
111+
);
105112

106113
setIdFromJdbc(instance, holder, persistentEntity);
107114

@@ -202,7 +209,7 @@ public <T> Iterable<T> findAllByProperty(Object rootId, JdbcPersistentProperty p
202209

203210
MapSqlParameterSource parameter = new MapSqlParameterSource(property.getReverseColumnName(), rootId);
204211

205-
return (Iterable<T>)operations.query(findAllByProperty, parameter, property.isQualified() //
212+
return (Iterable<T>) operations.query(findAllByProperty, parameter, property.isQualified() //
206213
? getMapEntityRowMapper(property) //
207214
: getEntityRowMapper(actualType));
208215
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.core;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import lombok.RequiredArgsConstructor;
22+
23+
import java.util.HashMap;
24+
25+
import org.junit.Test;
26+
import org.mockito.ArgumentCaptor;
27+
import org.springframework.data.annotation.Id;
28+
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
29+
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
30+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
31+
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
32+
import org.springframework.jdbc.support.KeyHolder;
33+
34+
/**
35+
* @author Jens Schauder
36+
*/
37+
public class DefaultDataAccessStrategyUnitTests {
38+
39+
public static final long ID_FROM_ADDITIONAL_VALUES = 23L;
40+
public static final long ORIGINAL_ID = 4711L;
41+
42+
JdbcMappingContext context = new JdbcMappingContext(new DefaultNamingStrategy());
43+
NamedParameterJdbcOperations jdbcOperations = mock(NamedParameterJdbcOperations.class);
44+
HashMap<String, Object> additionalParameters = new HashMap<>();
45+
ArgumentCaptor<SqlParameterSource> captor = ArgumentCaptor.forClass(SqlParameterSource.class);
46+
47+
DefaultDataAccessStrategy accessStrategy = new DefaultDataAccessStrategy( //
48+
new SqlGeneratorSource(context), //
49+
jdbcOperations, //
50+
context //
51+
);
52+
53+
@Test // DATAJDBC-146
54+
public void additionalParameterForIdDoesNotLeadToDuplicateParameters() {
55+
56+
additionalParameters.put("id", ID_FROM_ADDITIONAL_VALUES);
57+
58+
accessStrategy.insert(new DummyEntity(ORIGINAL_ID), DummyEntity.class, additionalParameters);
59+
60+
verify(jdbcOperations).update(eq("insert into DummyEntity (id) values (:id)"), captor.capture(),
61+
any(KeyHolder.class));
62+
assertThat(captor.getValue().getValue("id")).isEqualTo(ID_FROM_ADDITIONAL_VALUES);
63+
}
64+
65+
@Test // DATAJDBC-146
66+
public void additionalParametersGetAddedToStatement() {
67+
68+
additionalParameters.put("reference", ID_FROM_ADDITIONAL_VALUES);
69+
70+
accessStrategy.insert(new DummyEntity(ORIGINAL_ID), DummyEntity.class, additionalParameters);
71+
72+
verify(jdbcOperations).update(eq("insert into DummyEntity (id, reference) values (:id, :reference)"),
73+
captor.capture(), any(KeyHolder.class));
74+
assertThat(captor.getValue().getValue("id")).isEqualTo(ORIGINAL_ID);
75+
}
76+
77+
@RequiredArgsConstructor
78+
private static class DummyEntity {
79+
80+
@Id private final Long id;
81+
}
82+
83+
}

0 commit comments

Comments
 (0)