Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for ensureIndexes() logic #1211

Merged
merged 3 commits into from
Sep 24, 2018
Merged
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
4 changes: 3 additions & 1 deletion morphia/src/main/java/org/mongodb/morphia/IndexHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ private List<Index> collectNestedIndexes(final MappedClass mc, final List<Mapped
List<MappedClass> classes = new ArrayList<MappedClass>();
MappedClass mappedClass = mapper.getMappedClass(mf.isSingleValue() ? mf.getType() : mf.getSubClass());
classes.add(mappedClass);
classes.addAll(mapper.getSubTypes(mappedClass));
if (mappedClass.isInterface() || mappedClass.isAbstract()) {
classes.addAll(mapper.getSubTypes(mappedClass));
}
for (MappedClass aClass : classes) {
for (Index index : collectIndexes(aClass, parents)) {
List<Field> fields = new ArrayList<Field>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.mongodb.morphia.issue1175;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mongodb.morphia.testutil.IndexMatcher.doesNotHaveIndexNamed;
import static org.mongodb.morphia.testutil.IndexMatcher.hasIndexNamed;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.mongodb.morphia.TestBase;
import org.mongodb.morphia.annotations.Embedded;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Indexed;
import org.mongodb.morphia.utils.IndexDirection;

import com.mongodb.DBObject;

public class EnsureIndexesTest extends TestBase {
@Test
public final void ensureIndexesFromSuperclasses() {
// given
getMorphia().map(Person.class, Employee.class, Company.class);

// when
getAds().ensureIndexes();

// then
List<DBObject> indexInfo = getDs().getCollection(Company.class).getIndexInfo();
assertEquals(4, indexInfo.size());
assertThat(indexInfo, hasIndexNamed("_id_"));
assertThat(indexInfo, hasIndexNamed("employees.birthday_-1"));
assertThat(indexInfo, hasIndexNamed("employees.fullName_1"));
assertThat(indexInfo, hasIndexNamed("employees.employeeId_1"));
}

@Test
public final void ensureIndexesWithoutSubclasses() {
// given
getMorphia().map(Person.class, Employee.class, Contract.class);

// when
getAds().ensureIndexes();

// then
List<DBObject> indexInfo = getDs().getCollection(Contract.class).getIndexInfo();
assertEquals(3, indexInfo.size());
assertThat(indexInfo, hasIndexNamed("_id_"));
assertThat(indexInfo, hasIndexNamed("person.birthday_-1"));
assertThat(indexInfo, hasIndexNamed("person.fullName_1"));
assertThat(indexInfo, doesNotHaveIndexNamed("person.employeeId_1"));
}

@Embedded
public static class LivingBeing {
@Indexed(IndexDirection.DESC)
private Date birthday;
}

@Embedded
public static class Person extends LivingBeing {
@Indexed
private String fullName;
}

@Embedded
public static class Employee extends Person {
@Indexed
private Long employeeId;
private String title;
}

@Entity
public static class Contract {
@Id
private Long contractId;
private Long companyId;
@Embedded
private Person person;
}

@Entity
public static class Company {
@Id
private Long companyId;
@Embedded
private List<Employee> employees;
}
}