diff --git a/morphia/src/main/java/org/mongodb/morphia/DatastoreImpl.java b/morphia/src/main/java/org/mongodb/morphia/DatastoreImpl.java index 6997ae3605a..2abe6095e50 100644 --- a/morphia/src/main/java/org/mongodb/morphia/DatastoreImpl.java +++ b/morphia/src/main/java/org/mongodb/morphia/DatastoreImpl.java @@ -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; @@ -1026,10 +1027,18 @@ protected 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(); diff --git a/morphia/src/test/java/org/mongodb/morphia/mapping/CompoundIdTest.java b/morphia/src/test/java/org/mongodb/morphia/mapping/CompoundIdTest.java index a9465d71c76..4ca275c9703 100644 --- a/morphia/src/test/java/org/mongodb/morphia/mapping/CompoundIdTest.java +++ b/morphia/src/test/java/org/mongodb/morphia/mapping/CompoundIdTest.java @@ -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; @@ -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(); @@ -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 innerDAO = new BasicDAO(ConfigEntry.class, getDs()); + ConfigEntry entry = innerDAO.find().get(); + entry.setValue("something"); + innerDAO.save(entry); + } @Test @Ignore("https://github.com/mongodb/morphia/issues/675")