Browse Source

DATAMONGO-1590 - EntityInformation selected now correctly considers Persistable.

We now wrap the MappingMongoEntityInformation into one that delegates the methods implemented by Persistable to the actual entity in case it implements said interface.

Original pull request: #436.
pull/410/merge
Christoph Strobl 9 years ago committed by Oliver Gierke
parent
commit
f3b0665d94
  1. 55
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoEntityInformationSupport.java
  2. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java
  3. 102
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/PersistableMongoEntityInformation.java
  4. 76
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/PersistableMappingMongoEntityInformationUnitTests.java

55
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoEntityInformationSupport.java

@ -0,0 +1,55 @@ @@ -0,0 +1,55 @@
/*
* 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.mongodb.repository.support;
import java.io.Serializable;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Support class responsible for creating {@link MongoEntityInformation} instances for a given
* {@link MongoPersistentEntity}.
*
* @author Christoph Strobl
* @since 1.10
*/
final class MongoEntityInformationSupport {
private MongoEntityInformationSupport() {}
/**
* Factory method for creating {@link MongoEntityInformation}.
*
* @param entity must not be {@literal null}.
* @param idType can be {@literal null}.
* @return never {@literal null}.
*/
static <T, ID extends Serializable> MongoEntityInformation<T, ID> entityInformationFor(
MongoPersistentEntity<?> entity, Class<?> idType) {
Assert.notNull(entity, "Entity must not be null!");
MappingMongoEntityInformation entityInformation = new MappingMongoEntityInformation<T, ID>(
(MongoPersistentEntity<T>) entity, (Class<ID>) idType);
return ClassUtils.isAssignable(Persistable.class, entity.getType())
? new PersistableMongoEntityInformation<T, ID>(entityInformation) : entityInformation;
}
}

6
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java

@ -20,6 +20,7 @@ import static org.springframework.data.querydsl.QueryDslUtils.*; @@ -20,6 +20,7 @@ import static org.springframework.data.querydsl.QueryDslUtils.*;
import java.io.Serializable;
import java.lang.reflect.Method;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mongodb.core.MongoOperations;
@ -42,6 +43,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key; @@ -42,6 +43,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Factory to create {@link MongoRepository} instances.
@ -123,8 +125,8 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { @@ -123,8 +125,8 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
String.format("Could not lookup mapping metadata for domain class %s!", domainClass.getName()));
}
return new MappingMongoEntityInformation<T, ID>((MongoPersistentEntity<T>) entity,
information != null ? (Class<ID>) information.getIdType() : null);
return MongoEntityInformationSupport.<T, ID> entityInformationFor(entity,
information != null ? information.getIdType() : null);
}
/**

102
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/PersistableMongoEntityInformation.java

@ -0,0 +1,102 @@ @@ -0,0 +1,102 @@
/*
* 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.mongodb.repository.support;
import java.io.Serializable;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
/**
* {@link MongoEntityInformation} implementation wrapping an existing {@link MongoEntityInformation} considering
* {@link Persistable} types by delegating {@link #isNew(Object)} and {@link #getId(Object)} to the corresponding
* {@link Persistable#isNew()} and {@link Persistable#getId()} implementations.
*
* @author Christoph Strobl
* @since 1.10
*/
public class PersistableMongoEntityInformation<T, ID extends Serializable> implements MongoEntityInformation<T, ID> {
private final MongoEntityInformation<T, ID> delegate;
public PersistableMongoEntityInformation(MongoEntityInformation<T, ID> delegate) {
this.delegate = delegate;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.MongoEntityInformation#getCollectionName()
*/
@Override
public String getCollectionName() {
return delegate.getCollectionName();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.repository.MongoEntityInformation#getIdAttribute()
*/
@Override
public String getIdAttribute() {
return delegate.getIdAttribute();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.EntityInformation#isNew(java.lang.Object)
*/
@Override
public boolean isNew(T t) {
if (t instanceof Persistable) {
return ((Persistable) t).isNew();
}
return delegate.isNew(t);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
*/
@Override
public ID getId(T t) {
if (t instanceof Persistable) {
return (ID) ((Persistable) t).getId();
}
return delegate.getId(t);
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.PersistentEntityInformation#getIdType()
*/
@Override
public Class<ID> getIdType() {
return delegate.getIdType();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.EntityMetadata#getJavaType()
*/
@Override
public Class<T> getJavaType() {
return delegate.getJavaType();
}
}

76
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/PersistableMappingMongoEntityInformationUnitTests.java

@ -0,0 +1,76 @@ @@ -0,0 +1,76 @@
/*
* 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.mongodb.repository.query;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation;
import org.springframework.data.mongodb.repository.support.PersistableMongoEntityInformation;
/**
* Tests for {@link PersistableMongoEntityInformation}.
*
* @author Christoph Strobl
*/
@RunWith(MockitoJUnitRunner.class)
public class PersistableMappingMongoEntityInformationUnitTests {
@Mock MongoPersistentEntity<TypeImplementingPersistable> persistableImplementingEntityTypeInfo;
@Before
public void setUp() {
when(persistableImplementingEntityTypeInfo.getType()).thenReturn(TypeImplementingPersistable.class);
}
@Test // DATAMONGO-1590
public void considersPersistableIsNew() {
PersistableMongoEntityInformation<TypeImplementingPersistable, Long> information = new PersistableMongoEntityInformation<TypeImplementingPersistable, Long>(
new MappingMongoEntityInformation<TypeImplementingPersistable, Long>(persistableImplementingEntityTypeInfo));
assertThat(information.isNew(new TypeImplementingPersistable(100L, false)), is(false));
}
static class TypeImplementingPersistable implements Persistable<Long> {
final Long id;
final boolean isNew;
public TypeImplementingPersistable(Long id, boolean isNew) {
this.id = id;
this.isNew = isNew;
}
@Override
public Long getId() {
return id;
}
@Override
public boolean isNew() {
return isNew;
}
}
}
Loading…
Cancel
Save