Skip to content

Commit 163762e

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-1078 - @query annotated repository method fails for complex Id when used with Collection type.
Remove object type hint defaulting.
1 parent b99833d commit 163762e

File tree

6 files changed

+306
-2
lines changed

6 files changed

+306
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,7 @@ protected <S, T> List<T> doFind(String collectionName, DBObject query, DBObject
16221622

16231623
if (LOGGER.isDebugEnabled()) {
16241624
LOGGER.debug(String.format("find using query: %s fields: %s for class: %s in collection: %s",
1625-
serializeToJsonSafely(query), mappedFields, entityClass, collectionName));
1625+
serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName));
16261626
}
16271627

16281628
return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback,

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ public Object convertToMongoType(Object obj, TypeInformation<?> typeInformation)
944944
return getPotentiallyConvertedSimpleWrite(obj);
945945
}
946946

947-
TypeInformation<?> typeHint = typeInformation == null ? ClassTypeInformation.OBJECT : typeInformation;
947+
TypeInformation<?> typeHint = typeInformation;
948948

949949
if (obj instanceof BasicDBList) {
950950
return maybeConvertList((BasicDBList) obj, typeHint);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright 2014 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;
17+
18+
import static org.hamcrest.collection.IsCollectionWithSize.*;
19+
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
20+
import static org.hamcrest.core.IsEqual.*;
21+
import static org.junit.Assert.*;
22+
23+
import java.util.Collections;
24+
import java.util.List;
25+
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.context.annotation.Configuration;
31+
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
32+
import org.springframework.data.mongodb.core.MongoTemplate;
33+
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
34+
import org.springframework.test.context.ContextConfiguration;
35+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36+
37+
import com.mongodb.Mongo;
38+
import com.mongodb.MongoClient;
39+
40+
/**
41+
* @author Christoph Strobl
42+
*/
43+
@RunWith(SpringJUnit4ClassRunner.class)
44+
@ContextConfiguration
45+
public class ComplexIdRepositoryIntegrationTests {
46+
47+
@Configuration
48+
@EnableMongoRepositories
49+
static class Config extends AbstractMongoConfiguration {
50+
51+
@Override
52+
protected String getDatabaseName() {
53+
return "complexIdTest";
54+
}
55+
56+
@Override
57+
public Mongo mongo() throws Exception {
58+
return new MongoClient();
59+
}
60+
61+
}
62+
63+
@Autowired UserWithComplexIdRepository repo;
64+
65+
@Autowired MongoTemplate template;
66+
67+
private MyId id;
68+
private UserWithComplexId userWithId;
69+
70+
@Before
71+
public void setUp() {
72+
repo.deleteAll();
73+
74+
id = new MyId();
75+
id.val1 = "v1";
76+
id.val2 = "v2";
77+
78+
userWithId = new UserWithComplexId();
79+
userWithId.firstname = "foo";
80+
userWithId.id = id;
81+
}
82+
83+
/**
84+
* @see DATAMONGO-1078
85+
*/
86+
@Test
87+
public void annotatedFindQueryShouldWorkWhenUsingComplexId() {
88+
89+
repo.save(userWithId);
90+
91+
UserWithComplexId loaded = repo.getUserByComplexId(id);
92+
93+
assertThat(loaded, equalTo(userWithId));
94+
}
95+
96+
/**
97+
* @see DATAMONGO-1078
98+
*/
99+
@Test
100+
public void annotatedFindQueryShouldWorkWhenUsingComplexIdWithinCollection() {
101+
102+
repo.save(userWithId);
103+
104+
List<UserWithComplexId> loaded = repo.findByUserIds(Collections.singleton(id));
105+
106+
assertThat(loaded, hasSize(1));
107+
assertThat(loaded, contains(userWithId));
108+
}
109+
110+
/**
111+
* @see DATAMONGO-1078
112+
*/
113+
@Test
114+
public void findOneShouldWorkWhenUsingComplexId() {
115+
116+
repo.save(userWithId);
117+
118+
UserWithComplexId loaded = repo.findOne(id);
119+
120+
assertThat(loaded, equalTo(userWithId));
121+
}
122+
123+
/**
124+
* @see DATAMONGO-1078
125+
*/
126+
@Test
127+
public void findAllShouldWorkWhenUsingComplexId() {
128+
129+
repo.save(userWithId);
130+
131+
List<UserWithComplexId> loaded = (List<UserWithComplexId>) repo.findAll(Collections.singleton(id));
132+
133+
assertThat(loaded, hasSize(1));
134+
assertThat(loaded, contains(userWithId));
135+
}
136+
137+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2014 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;
17+
18+
import java.io.Serializable;
19+
20+
/**
21+
* @author Christoph Strobl
22+
*/
23+
public class MyId implements Serializable {
24+
25+
String val1;
26+
String val2;
27+
28+
@Override
29+
public int hashCode() {
30+
final int prime = 31;
31+
int result = 1;
32+
result = prime * result + ((val1 == null) ? 0 : val1.hashCode());
33+
result = prime * result + ((val2 == null) ? 0 : val2.hashCode());
34+
return result;
35+
}
36+
37+
@Override
38+
public boolean equals(Object obj) {
39+
if (this == obj) {
40+
return true;
41+
}
42+
if (obj == null) {
43+
return false;
44+
}
45+
if (!(obj instanceof MyId)) {
46+
return false;
47+
}
48+
MyId other = (MyId) obj;
49+
if (val1 == null) {
50+
if (other.val1 != null) {
51+
return false;
52+
}
53+
} else if (!val1.equals(other.val1)) {
54+
return false;
55+
}
56+
if (val2 == null) {
57+
if (other.val2 != null) {
58+
return false;
59+
}
60+
} else if (!val2.equals(other.val2)) {
61+
return false;
62+
}
63+
return true;
64+
}
65+
66+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2014 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;
17+
18+
import org.springframework.data.annotation.Id;
19+
import org.springframework.data.mongodb.core.mapping.Document;
20+
21+
/**
22+
* @author Christoph Strobl
23+
*/
24+
@Document
25+
public class UserWithComplexId {
26+
27+
@Id MyId id;
28+
String firstname;
29+
30+
@Override
31+
public int hashCode() {
32+
final int prime = 31;
33+
int result = 1;
34+
result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
35+
result = prime * result + ((id == null) ? 0 : id.hashCode());
36+
return result;
37+
}
38+
39+
@Override
40+
public boolean equals(Object obj) {
41+
if (this == obj) {
42+
return true;
43+
}
44+
if (obj == null) {
45+
return false;
46+
}
47+
if (!(obj instanceof UserWithComplexId)) {
48+
return false;
49+
}
50+
UserWithComplexId other = (UserWithComplexId) obj;
51+
if (firstname == null) {
52+
if (other.firstname != null) {
53+
return false;
54+
}
55+
} else if (!firstname.equals(other.firstname)) {
56+
return false;
57+
}
58+
if (id == null) {
59+
if (other.id != null) {
60+
return false;
61+
}
62+
} else if (!id.equals(other.id)) {
63+
return false;
64+
}
65+
return true;
66+
}
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2014 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;
17+
18+
import java.util.Collection;
19+
import java.util.List;
20+
21+
import org.springframework.data.repository.CrudRepository;
22+
23+
/**
24+
* @author Christoph Strobl
25+
*/
26+
public interface UserWithComplexIdRepository extends CrudRepository<UserWithComplexId, MyId> {
27+
28+
@Query("{'_id': {$in: ?0}}")
29+
List<UserWithComplexId> findByUserIds(Collection<MyId> ids);
30+
31+
@Query("{'_id': ?0}")
32+
UserWithComplexId getUserByComplexId(MyId id);
33+
}

0 commit comments

Comments
 (0)