Here are my (sample) objects. I haven't put any other annotations besides what's required for Morphia:
package jungle;
@Entity
public class Monkey {
String name;
int bananas;
@Embedded
TreeHouse house;
}
And the TreeHouse
object:
@Embedded
public class TreeHouse {
String type;
int distanceFromWater;
}
I'm trying to query on the type
by using a regex. Here's the MongoDB query that I'm using (and has been proven to work through the command line):
db.Monkey.find({ "house.type": { "$regex" : ".*coco.*", "$options": "i"}})
I'm able to generate this exact String in Java using the filter
method fro开发者_JS百科m a Query
object:
Query query = ...;
query = query.filter("house.type",
Pattern.compile(".*coco.*", Pattern.CASE_INSENSITIVE));
However, when I try to run search in Java, I get a ValidationException
:
com.google.code.morphia.query.ValidationException: The field 'house' could not be
found in 'jungle.Monkey' while validating - house.house.type; if you wish to
continue please disable validation.
Note the doubling of house.house.type
.
I'm using version 0.99 of Morphia, and using version 2.5 of the MongoDB Java driver. Am I not doing something correctly? Or is this a problem that has been fixed in a newer version?
Try this trick, it works for me:
query = query.disableValidation().filter("house.type",
Pattern.compile(".*coco.*", Pattern.CASE_INSENSITIVE));
精彩评论