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?