Skip to content

Commit

Permalink
make the ID field check smarter
Browse files Browse the repository at this point in the history
fixes #1104
  • Loading branch information
evanchooly committed Nov 13, 2018
1 parent 0321a81 commit 31c5609
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions morphia/src/main/java/xyz/morphia/mapping/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -634,7 +634,9 @@ public Object toMongoObject(final MappedField mf, final MappedClass mc, final Ob
Reference refAnn = mf.getAnnotation(Reference.class);
Class<?> idType = null;
if (!mf.getType().equals(Key.class) && isMapped(mf.getType())) {
idType = getMappedClass(mf.getType()).getMappedIdField().getType();
final MappedClass mappedClass = getMappedClass(mf.getType());
final MappedField idField = mappedClass.getMappedIdField();
idType = idField != null ? idField.getType() : null;
}
boolean valueIsIdType = mappedValue.getClass().equals(idType);
if (refAnn != null) {
Expand Down

3 comments on commit 31c5609

@alejavigo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit fixes a NPE problem I have with version 1.3.2 (I can't change version number). How could I avoid it (besides using my custom version of Mapper.java inside my project...)

@evanchooly
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I recall (this was over 2 years ago now), that issue arose when a object without an ID field made it to that point in the Mapper. In general, that "shouldn't happen" but clearly it did in some cases. So, "avoid that" seems the obvious but not exactly helpful answer. I would seriously consider upgrading, though. A lot of bug fixes have gone in since then and the impending 2.0 release will hopefully be the best one yet.

@alejavigo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's happening when I have a List of Entities with @reference, it tries to get the ID of the List itself...
I will fix it by turning OFF the logger in the Mapper class, as the query works ok but just logs the error. I have no other choice

Please sign in to comment.