0

I keep getting the same error whenever I try to delete an entity using Morphia:

org.mongodb.morphia.query.ValidationException: The field '_id' could not be found in ...
while validating - _id; if you wish to continue please disable validation.

I don't really want to disable validation. I just want to delete the object.


Here is my delete method of the service I'm writing:

public void delete(ObjectId id) {
    BaseMaterial baseMaterial = this.findOne(id);
    WriteResult writeResult = this.repo.delete(baseMaterial);
}

and here is the findOne method in the same service (i.e. this.findOne)

public BaseMaterial findOne(ObjectId id) {
    Query<BaseMaterial> query = repo.createQuery(BaseMaterial.class);
    return Optional.ofNullable(query.field("id").equal(id).get())
            .orElseThrow(() -> new DataRetrievalFailureException(
                    "Failed to fetch " + this.getClass().getName() + " with id " + id.toString()));
}

And here is my POJO:

@Entity("baseMaterial")
public class BaseMaterial {
    @Id
    @NotNull
    protected ObjectId id;
    @NotEmpty
    private String name;
    private String description;

    public String get_id() {
        return this.id.toString();
    }

    @JsonIgnore
    public ObjectId getId() {
        return id;
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

The findOne method of shown above works as expected so I really have no idea why this is happening.

Any ideas?

Rico Kahler
  • 17,616
  • 11
  • 59
  • 85

1 Answers1

1

I don't see a field annotated with @Id...

evanchooly
  • 6,102
  • 1
  • 16
  • 23
  • I did have the `@Id` annotation in ther. I inherited it from another class but for this question, I brought all of those fields into a the single class to keep it simple--it's a typo. I'm also currently debugging it without inheritance though. – Rico Kahler Nov 01 '17 at 11:55
  • I am still getting the same error with that field. I think i'm giving up and using spring's mongo object mapper. thanks for the catch though – Rico Kahler Nov 01 '17 at 11:56
  • I'm getting on a plane now but can look later if you'd like. – evanchooly Nov 01 '17 at 12:00