Skip to content

Commit

Permalink
tweaks to PR #771 and merge
Browse files Browse the repository at this point in the history
this change disables validation against the ID field when the field type is not "primitive like" and the idValue is an instance of DBObject suggesting that it's a complex key that has been converted to its DBObject form
(cherry picked from commit aef4b69)
  • Loading branch information
Justin Lee committed Jun 8, 2015
1 parent b873234 commit 833fd62
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 4 deletions.
17 changes: 13 additions & 4 deletions morphia/src/main/java/org/mongodb/morphia/DatastoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.mongodb.morphia.query.UpdateResults;
import org.mongodb.morphia.utils.Assert;
import org.mongodb.morphia.utils.IndexType;
import org.mongodb.morphia.utils.ReflectionUtils;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
Expand Down Expand Up @@ -1026,10 +1027,18 @@ protected <T> WriteResult tryVersionedUpdate(final DBCollection dbColl, final T
mfVersion.setFieldValue(entity, newVersion);

if (idValue != null && newVersion != 1) {
final UpdateResults res = update(
find(dbColl.getName(), entity.getClass()).filter(Mapper.ID_KEY, idValue)
.filter(versionKeyName, oldVersion), dbObj,
false, false, wc);
final Query<?> query = find(dbColl.getName(), entity.getClass());
boolean compoundId = !ReflectionUtils.isPrimitiveLike(mc.getMappedIdField().getType())
&& idValue instanceof DBObject;
if (compoundId) {
query.disableValidation();
}
query.filter(Mapper.ID_KEY, idValue);
if (compoundId) {
query.enableValidation();
}
query.filter(versionKeyName, oldVersion);
final UpdateResults res = update(query, dbObj, false, false, wc);

wr = res.getWriteResult();

Expand Down
113 changes: 113 additions & 0 deletions morphia/src/test/java/org/mongodb/morphia/mapping/CompoundIdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import org.mongodb.morphia.AdvancedDatastore;
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.Reference;
import org.mongodb.morphia.annotations.Version;
import org.mongodb.morphia.dao.BasicDAO;

import java.io.Serializable;

Expand Down Expand Up @@ -83,7 +86,108 @@ public int hashCode() {
}
}

public static class ConfigKey {
private String env;
private String subenv;
private String key;

public ConfigKey() {
}

public ConfigKey(final String env, final String key, final String subenv) {
this.env = env;
this.key = key;
this.subenv = subenv;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

final ConfigKey configKey = (ConfigKey) o;

if (!env.equals(configKey.env)) {
return false;
}
if (!subenv.equals(configKey.subenv)) {
return false;
}
return key.equals(configKey.key);

}

@Override
public int hashCode() {
int result = env.hashCode();
result = 31 * result + subenv.hashCode();
result = 31 * result + key.hashCode();
return result;
}
}

@Entity(noClassnameStored = true)
public static class ConfigEntry {
@Id
private ConfigKey key;
private String value;
@Version
private long version;
private String lastModifiedUser;
private long lastModifiedMillis;

public ConfigEntry() {
}

public ConfigEntry(final ConfigKey key) {
this.key = key;
}

public ConfigKey getKey() {
return key;
}

public void setKey(final ConfigKey key) {
this.key = key;
}

public long getLastModifiedMillis() {
return lastModifiedMillis;
}

public void setLastModifiedMillis(final long lastModifiedMillis) {
this.lastModifiedMillis = lastModifiedMillis;
}

public String getLastModifiedUser() {
return lastModifiedUser;
}

public void setLastModifiedUser(final String lastModifiedUser) {
this.lastModifiedUser = lastModifiedUser;
}

public String getValue() {
return value;
}

public void setValue(final String value) {
this.value = value;
}

public long getVersion() {
return version;
}

public void setVersion(final long version) {
this.version = version;
}
}

@Test
public void testMapping() throws Exception {
CompoundIdEntity entity = new CompoundIdEntity();
Expand Down Expand Up @@ -112,6 +216,15 @@ public void testOtherDelete() throws Exception {
getDs().save(entity);
((AdvancedDatastore) getDs()).delete(getDs().getCollection(CompoundIdEntity.class).getName(), CompoundIdEntity.class, entity.id);
}

@Test
public void testFetchKey() {
getDs().save(new ConfigEntry(new ConfigKey("env", "key", "subenv")));
BasicDAO<ConfigEntry, ConfigKey> innerDAO = new BasicDAO<ConfigEntry, ConfigKey>(ConfigEntry.class, getDs());
ConfigEntry entry = innerDAO.find().get();
entry.setValue("something");
innerDAO.save(entry);
}

@Test
@Ignore("https://github.com/mongodb/morphia/issues/675")
Expand Down

0 comments on commit 833fd62

Please sign in to comment.