DATAMONGO-1078 - @Query annotated repository method fails for complex Id when used with Collection type.

Remove object type hint defaulting.
This commit is contained in:
Christoph Strobl
2014-10-28 12:54:49 +01:00
committed by Oliver Gierke
parent b99833df75
commit 163762e99e
6 changed files with 306 additions and 2 deletions

View File

@@ -1622,7 +1622,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("find using query: %s fields: %s for class: %s in collection: %s",
serializeToJsonSafely(query), mappedFields, entityClass, collectionName));
serializeToJsonSafely(mappedQuery), mappedFields, entityClass, collectionName));
}
return executeFindMultiInternal(new FindCallback(mappedQuery, mappedFields), preparer, objectCallback,

View File

@@ -944,7 +944,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
return getPotentiallyConvertedSimpleWrite(obj);
}
TypeInformation<?> typeHint = typeInformation == null ? ClassTypeInformation.OBJECT : typeInformation;
TypeInformation<?> typeHint = typeInformation;
if (obj instanceof BasicDBList) {
return maybeConvertList((BasicDBList) obj, typeHint);

View File

@@ -0,0 +1,137 @@
/*
* Copyright 2014 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;
import static org.hamcrest.collection.IsCollectionWithSize.*;
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
import static org.hamcrest.core.IsEqual.*;
import static org.junit.Assert.*;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
/**
* @author Christoph Strobl
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ComplexIdRepositoryIntegrationTests {
@Configuration
@EnableMongoRepositories
static class Config extends AbstractMongoConfiguration {
@Override
protected String getDatabaseName() {
return "complexIdTest";
}
@Override
public Mongo mongo() throws Exception {
return new MongoClient();
}
}
@Autowired UserWithComplexIdRepository repo;
@Autowired MongoTemplate template;
private MyId id;
private UserWithComplexId userWithId;
@Before
public void setUp() {
repo.deleteAll();
id = new MyId();
id.val1 = "v1";
id.val2 = "v2";
userWithId = new UserWithComplexId();
userWithId.firstname = "foo";
userWithId.id = id;
}
/**
* @see DATAMONGO-1078
*/
@Test
public void annotatedFindQueryShouldWorkWhenUsingComplexId() {
repo.save(userWithId);
UserWithComplexId loaded = repo.getUserByComplexId(id);
assertThat(loaded, equalTo(userWithId));
}
/**
* @see DATAMONGO-1078
*/
@Test
public void annotatedFindQueryShouldWorkWhenUsingComplexIdWithinCollection() {
repo.save(userWithId);
List<UserWithComplexId> loaded = repo.findByUserIds(Collections.singleton(id));
assertThat(loaded, hasSize(1));
assertThat(loaded, contains(userWithId));
}
/**
* @see DATAMONGO-1078
*/
@Test
public void findOneShouldWorkWhenUsingComplexId() {
repo.save(userWithId);
UserWithComplexId loaded = repo.findOne(id);
assertThat(loaded, equalTo(userWithId));
}
/**
* @see DATAMONGO-1078
*/
@Test
public void findAllShouldWorkWhenUsingComplexId() {
repo.save(userWithId);
List<UserWithComplexId> loaded = (List<UserWithComplexId>) repo.findAll(Collections.singleton(id));
assertThat(loaded, hasSize(1));
assertThat(loaded, contains(userWithId));
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2014 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;
import java.io.Serializable;
/**
* @author Christoph Strobl
*/
public class MyId implements Serializable {
String val1;
String val2;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((val1 == null) ? 0 : val1.hashCode());
result = prime * result + ((val2 == null) ? 0 : val2.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof MyId)) {
return false;
}
MyId other = (MyId) obj;
if (val1 == null) {
if (other.val1 != null) {
return false;
}
} else if (!val1.equals(other.val1)) {
return false;
}
if (val2 == null) {
if (other.val2 != null) {
return false;
}
} else if (!val2.equals(other.val2)) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2014 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;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author Christoph Strobl
*/
@Document
public class UserWithComplexId {
@Id MyId id;
String firstname;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof UserWithComplexId)) {
return false;
}
UserWithComplexId other = (UserWithComplexId) obj;
if (firstname == null) {
if (other.firstname != null) {
return false;
}
} else if (!firstname.equals(other.firstname)) {
return false;
}
if (id == null) {
if (other.id != null) {
return false;
}
} else if (!id.equals(other.id)) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2014 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;
import java.util.Collection;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
/**
* @author Christoph Strobl
*/
public interface UserWithComplexIdRepository extends CrudRepository<UserWithComplexId, MyId> {
@Query("{'_id': {$in: ?0}}")
List<UserWithComplexId> findByUserIds(Collection<MyId> ids);
@Query("{'_id': ?0}")
UserWithComplexId getUserByComplexId(MyId id);
}