Skip to content

Commit 63d9dcc

Browse files
christophstroblodrotbohm
authored andcommitted
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.
1 parent 7c4b5a1 commit 63d9dcc

File tree

4 files changed

+237
-2
lines changed

4 files changed

+237
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.mongodb.repository.support;
17+
18+
import java.io.Serializable;
19+
20+
import org.springframework.data.domain.Persistable;
21+
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
22+
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
23+
import org.springframework.util.Assert;
24+
import org.springframework.util.ClassUtils;
25+
26+
/**
27+
* Support class responsible for creating {@link MongoEntityInformation} instances for a given
28+
* {@link MongoPersistentEntity}.
29+
*
30+
* @author Christoph Strobl
31+
* @since 1.10
32+
*/
33+
final class MongoEntityInformationSupport {
34+
35+
private MongoEntityInformationSupport() {}
36+
37+
/**
38+
* Factory method for creating {@link MongoEntityInformation}.
39+
*
40+
* @param entity must not be {@literal null}.
41+
* @param idType can be {@literal null}.
42+
* @return never {@literal null}.
43+
*/
44+
static <T, ID extends Serializable> MongoEntityInformation<T, ID> entityInformationFor(
45+
MongoPersistentEntity<?> entity, Class<?> idType) {
46+
47+
Assert.notNull(entity, "Entity must not be null!");
48+
49+
MappingMongoEntityInformation entityInformation = new MappingMongoEntityInformation<T, ID>(
50+
(MongoPersistentEntity<T>) entity, (Class<ID>) idType);
51+
return ClassUtils.isAssignable(Persistable.class, entity.getType())
52+
? new PersistableMongoEntityInformation<T, ID>(entityInformation) : entityInformation;
53+
}
54+
55+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.Serializable;
2121
import java.lang.reflect.Method;
2222

23+
import org.springframework.data.domain.Persistable;
2324
import org.springframework.data.mapping.context.MappingContext;
2425
import org.springframework.data.mapping.model.MappingException;
2526
import org.springframework.data.mongodb.core.MongoOperations;
@@ -41,6 +42,7 @@
4142
import org.springframework.data.repository.query.RepositoryQuery;
4243
import org.springframework.expression.spel.standard.SpelExpressionParser;
4344
import org.springframework.util.Assert;
45+
import org.springframework.util.ClassUtils;
4446

4547
/**
4648
* Factory to create {@link MongoRepository} instances.
@@ -122,8 +124,8 @@ private <T, ID extends Serializable> MongoEntityInformation<T, ID> getEntityInfo
122124
String.format("Could not lookup mapping metadata for domain class %s!", domainClass.getName()));
123125
}
124126

125-
return new MappingMongoEntityInformation<T, ID>((MongoPersistentEntity<T>) entity,
126-
information != null ? (Class<ID>) information.getIdType() : null);
127+
return MongoEntityInformationSupport.<T, ID> entityInformationFor(entity,
128+
information != null ? information.getIdType() : null);
127129
}
128130

129131
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.mongodb.repository.support;
17+
18+
import java.io.Serializable;
19+
20+
import org.springframework.data.domain.Persistable;
21+
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
22+
23+
/**
24+
* {@link MongoEntityInformation} implementation wrapping an existing {@link MongoEntityInformation} considering
25+
* {@link Persistable} types by delegating {@link #isNew(Object)} and {@link #getId(Object)} to the corresponding
26+
* {@link Persistable#isNew()} and {@link Persistable#getId()} implementations.
27+
*
28+
* @author Christoph Strobl
29+
* @since 1.10
30+
*/
31+
public class PersistableMongoEntityInformation<T, ID extends Serializable> implements MongoEntityInformation<T, ID> {
32+
33+
private final MongoEntityInformation<T, ID> delegate;
34+
35+
public PersistableMongoEntityInformation(MongoEntityInformation<T, ID> delegate) {
36+
this.delegate = delegate;
37+
}
38+
39+
/*
40+
* (non-Javadoc)
41+
* @see org.springframework.data.mongodb.repository.MongoEntityInformation#getCollectionName()
42+
*/
43+
@Override
44+
public String getCollectionName() {
45+
return delegate.getCollectionName();
46+
}
47+
48+
/*
49+
* (non-Javadoc)
50+
* @see org.springframework.data.mongodb.repository.MongoEntityInformation#getIdAttribute()
51+
*/
52+
@Override
53+
public String getIdAttribute() {
54+
return delegate.getIdAttribute();
55+
}
56+
57+
/*
58+
* (non-Javadoc)
59+
* @see org.springframework.data.repository.core.EntityInformation#isNew(java.lang.Object)
60+
*/
61+
@Override
62+
public boolean isNew(T t) {
63+
64+
if (t instanceof Persistable) {
65+
return ((Persistable) t).isNew();
66+
}
67+
68+
return delegate.isNew(t);
69+
}
70+
71+
/*
72+
* (non-Javadoc)
73+
* @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
74+
*/
75+
@Override
76+
public ID getId(T t) {
77+
78+
if (t instanceof Persistable) {
79+
return (ID) ((Persistable) t).getId();
80+
}
81+
82+
return delegate.getId(t);
83+
}
84+
85+
/*
86+
* (non-Javadoc)
87+
* @see org.springframework.data.repository.core.support.PersistentEntityInformation#getIdType()
88+
*/
89+
@Override
90+
public Class<ID> getIdType() {
91+
return delegate.getIdType();
92+
}
93+
94+
/*
95+
* (non-Javadoc)
96+
* @see org.springframework.data.repository.core.support.EntityMetadata#getJavaType()
97+
*/
98+
@Override
99+
public Class<T> getJavaType() {
100+
return delegate.getJavaType();
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.mongodb.repository.query;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
import static org.mockito.Mockito.*;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.mockito.Mock;
26+
import org.mockito.runners.MockitoJUnitRunner;
27+
import org.springframework.data.domain.Persistable;
28+
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
29+
import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation;
30+
import org.springframework.data.mongodb.repository.support.PersistableMongoEntityInformation;
31+
32+
/**
33+
* Tests for {@link PersistableMongoEntityInformation}.
34+
*
35+
* @author Christoph Strobl
36+
*/
37+
@RunWith(MockitoJUnitRunner.class)
38+
public class PersistableMappingMongoEntityInformationUnitTests {
39+
40+
@Mock MongoPersistentEntity<TypeImplementingPersistable> persistableImplementingEntityTypeInfo;
41+
42+
@Before
43+
public void setUp() {
44+
when(persistableImplementingEntityTypeInfo.getType()).thenReturn(TypeImplementingPersistable.class);
45+
}
46+
47+
@Test // DATAMONGO-1590
48+
public void considersPersistableIsNew() {
49+
50+
PersistableMongoEntityInformation<TypeImplementingPersistable, Long> information = new PersistableMongoEntityInformation<TypeImplementingPersistable, Long>(
51+
new MappingMongoEntityInformation<TypeImplementingPersistable, Long>(persistableImplementingEntityTypeInfo));
52+
53+
assertThat(information.isNew(new TypeImplementingPersistable(100L, false)), is(false));
54+
}
55+
56+
static class TypeImplementingPersistable implements Persistable<Long> {
57+
58+
final Long id;
59+
final boolean isNew;
60+
61+
public TypeImplementingPersistable(Long id, boolean isNew) {
62+
this.id = id;
63+
this.isNew = isNew;
64+
}
65+
66+
@Override
67+
public Long getId() {
68+
return id;
69+
}
70+
71+
@Override
72+
public boolean isNew() {
73+
return isNew;
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)