Skip to content
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 @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-384-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-384-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-384-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-384-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ static <T> NoValuePropertyAccessor<T> instance() {
}

@Override
public void setProperty(PersistentProperty<?> property, Object value) {
public void setProperty(PersistentProperty<?> property, @Nullable Object value) {
throw new UnsupportedOperationException("Cannot set value on 'null' target object.");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@org.springframework.lang.NonNullApi
package org.springframework.data.jdbc.core.mapping;
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package org.springframework.data.jdbc.mybatis;

import java.util.Collections;
import java.util.Map;

import org.springframework.data.relational.domain.Identifier;
import org.springframework.lang.Nullable;

/**
Expand All @@ -25,23 +27,35 @@
* the kind of values available on invocation.
*
* @author Jens Schauder
* @author Christoph Strobl
*/
public class MyBatisContext {

private final Object id;
private final Object instance;
private final Class domainType;
private final @Nullable Object id;
private final @Nullable Object instance;
private final @Nullable Identifier identifier;
private final @Nullable Class domainType;
private final Map<String, Object> additonalValues;

public MyBatisContext(@Nullable Object id, @Nullable Object instance, Class domainType,
public MyBatisContext(@Nullable Object id, @Nullable Object instance, @Nullable Class domainType,
Map<String, Object> additonalValues) {

this.id = id;
this.identifier = null;
this.instance = instance;
this.domainType = domainType;
this.additonalValues = additonalValues;
}

public MyBatisContext(Identifier identifier, @Nullable Object instance, @Nullable Class<?> domainType) {

this.id = null;
this.identifier = identifier;
this.instance = instance;
this.domainType = domainType;
this.additonalValues = Collections.emptyMap();
}

/**
* The ID of the entity to query/act upon.
*
Expand All @@ -52,6 +66,16 @@ public Object getId() {
return id;
}

/**
* The {@link Identifier} for a path to query.
*
* @return Might return {@literal null}.
*/
@Nullable
public Identifier getIdentifier() {
return identifier;
}

/**
* The entity to act upon. This is {@code null} for queries, since the object doesn't exist before the query.
*
Expand Down Expand Up @@ -82,4 +106,5 @@ public Class getDomainType() {
public Object get(String key) {
return additonalValues.get(key);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import java.util.Collections;
import java.util.Map;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jdbc.core.convert.CascadingDataAccessStrategy;
import org.springframework.data.jdbc.core.convert.DataAccessStrategy;
import org.springframework.data.jdbc.core.convert.DefaultDataAccessStrategy;
Expand Down Expand Up @@ -52,6 +55,8 @@
*/
public class MyBatisDataAccessStrategy implements DataAccessStrategy {

private static final Logger LOG = LoggerFactory.getLogger(MyBatisDataAccessStrategy.class);

private final SqlSession sqlSession;
private NamespaceStrategy namespaceStrategy = NamespaceStrategy.DEFAULT_INSTANCE;

Expand Down Expand Up @@ -245,8 +250,20 @@ public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
@Override
public Iterable<Object> findAllByPath(Identifier identifier,
PersistentPropertyPath<RelationalPersistentProperty> path) {
return sqlSession().selectList(namespace(path.getBaseProperty().getOwner().getType()) + ".findAllByPath",
new MyBatisContext(identifier, null, path.getLeafProperty().getType(), Collections.emptyMap()));

String statementName = namespace(path.getBaseProperty().getOwner().getType()) + ".findAllByPath-"
+ path.toDotPath();

try {
return sqlSession().selectList(statementName,
new MyBatisContext(identifier, null, path.getRequiredLeafProperty().getType()));
} catch (PersistenceException pex) {

LOG.debug(String.format("Didn't find %s in the MyBatis session. Falling back to findAllByPath.", statementName),
pex);

return DataAccessStrategy.super.findAllByPath(identifier, path);
}
}

/*
Expand All @@ -255,6 +272,7 @@ public Iterable<Object> findAllByPath(Identifier identifier,
*/
@Override
public <T> Iterable<T> findAllByProperty(Object rootId, RelationalPersistentProperty property) {

return sqlSession().selectList(
namespace(property.getOwner().getType()) + ".findAllByProperty-" + property.getName(),
new MyBatisContext(rootId, null, property.getType(), Collections.emptyMap()));
Expand All @@ -266,6 +284,7 @@ public <T> Iterable<T> findAllByProperty(Object rootId, RelationalPersistentProp
*/
@Override
public <T> boolean existsById(Object id, Class<T> domainType) {

return sqlSession().selectOne(namespace(domainType) + ".existsById",
new MyBatisContext(id, null, domainType, Collections.emptyMap()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@
package org.springframework.data.jdbc.mybatis;

import static java.util.Arrays.*;
import static java.util.Collections.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.util.Collections;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import org.springframework.data.jdbc.core.PropertyPathTestingUtils;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.domain.Identifier;

/**
* Unit tests for the {@link MyBatisDataAccessStrategy}, mainly ensuring that the correct statements get's looked up.
Expand All @@ -48,7 +50,8 @@ public class MyBatisDataAccessStrategyUnitTests {

MyBatisDataAccessStrategy accessStrategy = new MyBatisDataAccessStrategy(session);

PersistentPropertyPath<RelationalPersistentProperty> path = PropertyPathTestingUtils.toPath("one.two", DummyEntity.class, context);
PersistentPropertyPath<RelationalPersistentProperty> path = PropertyPathTestingUtils.toPath("one.two",
DummyEntity.class, context);

@Before
public void before() {
Expand Down Expand Up @@ -127,8 +130,8 @@ public void deleteAllByPath() {

accessStrategy.deleteAll(path);

verify(session).delete(
eq("org.springframework.data.jdbc.mybatis.MyBatisDataAccessStrategyUnitTests$DummyEntityMapper.deleteAll-one-two"),
verify(session).delete(eq(
"org.springframework.data.jdbc.mybatis.MyBatisDataAccessStrategyUnitTests$DummyEntityMapper.deleteAll-one-two"),
captor.capture());

assertThat(captor.getValue()) //
Expand Down Expand Up @@ -285,6 +288,65 @@ public void findAllByProperty() {
);
}

@SuppressWarnings("unchecked")
@Test // DATAJDBC-384
public void findAllByPath() {

RelationalPersistentProperty property = mock(RelationalPersistentProperty.class, RETURNS_DEEP_STUBS);
PersistentPropertyPath path = mock(PersistentPropertyPath.class, RETURNS_DEEP_STUBS);

when(path.getBaseProperty()).thenReturn(property);
when(property.getOwner().getType()).thenReturn((Class) String.class);

when(path.getRequiredLeafProperty()).thenReturn(property);
when(property.getType()).thenReturn((Class) Number.class);

when(path.toDotPath()).thenReturn("dot.path");

accessStrategy.findAllByPath(Identifier.empty(), path);

verify(session).selectList(eq("java.lang.StringMapper.findAllByPath-dot.path"), captor.capture());

assertThat(captor.getValue()) //
.isNotNull() //
.extracting( //
MyBatisContext::getInstance, //
MyBatisContext::getId, //
MyBatisContext::getIdentifier, //
MyBatisContext::getDomainType, //
c -> c.get("key") //
).containsExactly( //
null, //
null, //
Identifier.empty(), //
Number.class, //
null //
);
}

@SuppressWarnings("unchecked")
@Test // DATAJDBC-384
public void findAllByPathFallsBackToFindAllByProperty() {

RelationalPersistentProperty property = mock(RelationalPersistentProperty.class, RETURNS_DEEP_STUBS);
PersistentPropertyPath path = mock(PersistentPropertyPath.class, RETURNS_DEEP_STUBS);

when(path.getBaseProperty()).thenReturn(property);
when(property.getOwner().getType()).thenReturn((Class) String.class);

when(path.getRequiredLeafProperty()).thenReturn(property);
when(property.getType()).thenReturn((Class) Number.class);

when(path.toDotPath()).thenReturn("dot.path");

when(session.selectList(any(), any())).thenThrow(PersistenceException.class).thenReturn(emptyList());

accessStrategy.findAllByPath(Identifier.empty(), path);

verify(session, times(2)).selectList(any(), any());

}

@Test // DATAJDBC-123
public void existsById() {

Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-384-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<version>1.1.0.DATAJDBC-384-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract class AbstractImportValidator implements Visitor {

Set<Table> requiredByWhere = new HashSet<>();
Set<Table> from = new HashSet<>();
Visitable parent;
@Nullable Visitable parent;

/*
* (non-Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ static class NamedBindMarker extends BindMarker implements Named {
* (non-Javadoc)
* @see org.springframework.data.relational.core.sql.Named#getName()
*/
@Nullable
@Override
public String getName() {
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
class DefaultDeleteBuilder implements DeleteBuilder, DeleteBuilder.DeleteWhereAndOr, DeleteBuilder.DeleteWhere {

private Table from;
private @Nullable Table from;
private @Nullable Condition where;

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Collection;
import java.util.List;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
Expand All @@ -31,7 +32,7 @@
class DefaultInsertBuilder
implements InsertBuilder, InsertBuilder.InsertIntoColumnsAndValuesWithBuild, InsertBuilder.InsertValuesWithBuild {

private Table into;
private @Nullable Table into;
private List<Column> columns = new ArrayList<>();
private List<Expression> values = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,15 @@ static class JoinBuilder implements SelectOn, SelectOnConditionComparison, Selec
private final Table table;
private final DefaultSelectBuilder selectBuilder;
private final JoinType joinType;
private Expression from;
private Expression to;
private @Nullable Expression from;
private @Nullable Expression to;
private @Nullable Condition condition;


JoinBuilder(Table table, DefaultSelectBuilder selectBuilder, JoinType joinType) {

this.table = table;
this.selectBuilder = selectBuilder;

this.joinType = joinType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/
class DefaultUpdateBuilder implements UpdateBuilder, UpdateWhere, UpdateWhereAndOr, UpdateAssign {

private Table table;
private @Nullable Table table;
private List<Assignment> assignments = new ArrayList<>();
private @Nullable Condition where;

Expand Down
Loading