Browse Source
Added a property path to DbActions in order to make them aware of the property it is related to. Then used the JdbcPersistentProperty based on that get the name of the back-reference name of that property back to the root entity of that property. This gets the NamingStrategy properly involved, as it should. Instead of using the vanilla PropertyPath a new JdbcPropertyPath is introduced. It allows for an empty path avoiding various null-checks. Upgraded SD-Commons dependency to 2.1.x in order to utilise DATACMNS-1199. Removed the now superfluous PropertyPaths and used PropertyPath methods instead. Related issues: DATACMNS-1204 DATACMSN-1199pull/22/merge
13 changed files with 282 additions and 163 deletions
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
/* |
||||
* Copyright 2017 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 org.springframework.data.jdbc.core.conversion; |
||||
|
||||
import org.springframework.data.mapping.PropertyPath; |
||||
import org.springframework.util.StringUtils; |
||||
|
||||
/** |
||||
* A replacement for {@link org.springframework.data.mapping.PropertyPath} as long as it doesn't support objects with |
||||
* empty path. |
||||
* |
||||
* See https://jira.spring.io/browse/DATACMNS-1204.
|
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class JdbcPropertyPath { |
||||
|
||||
private final PropertyPath path; |
||||
private final Class<?> rootType; |
||||
|
||||
JdbcPropertyPath(PropertyPath path) { |
||||
|
||||
this.path = path; |
||||
this.rootType = null; |
||||
} |
||||
|
||||
private JdbcPropertyPath(Class<?> type) { |
||||
|
||||
this.path = null; |
||||
this.rootType = type; |
||||
} |
||||
|
||||
public static JdbcPropertyPath from(String source, Class<?> type) { |
||||
|
||||
if (StringUtils.isEmpty(source)) { |
||||
return new JdbcPropertyPath(type); |
||||
} else { |
||||
return new JdbcPropertyPath(PropertyPath.from(source, type)); |
||||
} |
||||
} |
||||
|
||||
public JdbcPropertyPath nested(String name) { |
||||
return path == null ? new JdbcPropertyPath(PropertyPath.from(name, rootType)) : new JdbcPropertyPath(path.nested(name)); |
||||
} |
||||
|
||||
public PropertyPath getPath() { |
||||
return path; |
||||
} |
||||
|
||||
public String toDotPath() { |
||||
return path == null ? "" : path.toDotPath(); |
||||
} |
||||
} |
||||
@ -1,46 +0,0 @@
@@ -1,46 +0,0 @@
|
||||
/* |
||||
* Copyright 2017 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 org.springframework.data.jdbc.mapping.model; |
||||
|
||||
import lombok.experimental.UtilityClass; |
||||
|
||||
import org.springframework.data.mapping.PropertyPath; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Utilities for working with {@link PropertyPath}s. |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
@UtilityClass |
||||
public class PropertyPaths { |
||||
|
||||
public static Class<?> getLeafType(PropertyPath path) { |
||||
|
||||
if (path.hasNext()) { |
||||
return getLeafType(path.next()); |
||||
} |
||||
return path.getType(); |
||||
} |
||||
|
||||
public static PropertyPath extendBy(PropertyPath path, String name) { |
||||
|
||||
Assert.notNull(path, "Path must not be null."); |
||||
Assert.hasText(name, "Name must not be empty"); |
||||
|
||||
return PropertyPath.from(path.toDotPath() + "." + name, path.getOwningType()); |
||||
} |
||||
} |
||||
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
/* |
||||
* Copyright 2017 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 org.springframework.data.jdbc.core; |
||||
|
||||
import static org.assertj.core.api.Assertions.*; |
||||
import static org.mockito.Mockito.*; |
||||
|
||||
import java.util.AbstractMap.SimpleEntry; |
||||
import java.util.Map; |
||||
|
||||
import org.junit.Test; |
||||
import org.mockito.ArgumentCaptor; |
||||
import org.springframework.data.annotation.Id; |
||||
import org.springframework.data.jdbc.core.conversion.DbAction; |
||||
import org.springframework.data.jdbc.core.conversion.DbAction.Insert; |
||||
import org.springframework.data.jdbc.core.conversion.JdbcPropertyPath; |
||||
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy; |
||||
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext; |
||||
import org.springframework.data.jdbc.mapping.model.JdbcPersistentProperty; |
||||
|
||||
/** |
||||
* Unit tests for {@link DefaultJdbcInterpreter} |
||||
* |
||||
* @author Jens Schauder |
||||
*/ |
||||
public class DefaultJdbcInterpreterUnitTests { |
||||
|
||||
static final long CONTAINER_ID = 23L; |
||||
static final String BACK_REFERENCE = "back-reference"; |
||||
|
||||
JdbcMappingContext context = new JdbcMappingContext(new DefaultNamingStrategy() { |
||||
@Override |
||||
public String getReverseColumnName(JdbcPersistentProperty property) { |
||||
return BACK_REFERENCE; |
||||
} |
||||
}); |
||||
|
||||
DataAccessStrategy dataAccessStrategy = mock(DataAccessStrategy.class); |
||||
DefaultJdbcInterpreter interpreter = new DefaultJdbcInterpreter(context, dataAccessStrategy); |
||||
|
||||
@Test // DATAJDBC-145
|
||||
public void insertDoesHonourNamingStrategyForBackReference() { |
||||
|
||||
Container container = new Container(); |
||||
container.id = CONTAINER_ID; |
||||
|
||||
Element element = new Element(); |
||||
|
||||
Insert<?> containerInsert = DbAction.insert(container, JdbcPropertyPath.from("", Container.class), null); |
||||
Insert<?> insert = DbAction.insert(element, JdbcPropertyPath.from("element", Container.class), containerInsert); |
||||
|
||||
interpreter.interpret(insert); |
||||
|
||||
ArgumentCaptor<Map<String, Object>> argumentCaptor = ArgumentCaptor.forClass(Map.class); |
||||
verify(dataAccessStrategy).insert(eq(element), eq(Element.class), argumentCaptor.capture()); |
||||
|
||||
assertThat(argumentCaptor.getValue()).containsExactly(new SimpleEntry(BACK_REFERENCE, CONTAINER_ID)); |
||||
} |
||||
|
||||
static class Container { |
||||
|
||||
@Id Long id; |
||||
|
||||
Element element; |
||||
} |
||||
|
||||
static class Element {} |
||||
} |
||||
Loading…
Reference in new issue