为什么 MongoDB Java 驱动程序/Morphia 会在属性前面添加两次?
这是我的(示例)对象。除了 Morphia 所需的注释之外,我没有添加任何其他注释:
package jungle;
@Entity
public class Monkey {
String name;
int bananas;
@Embedded
TreeHouse house;
}
和 TreeHouse
对象:
@Embedded
public class TreeHouse {
String type;
int distanceFromWater;
}
我正在尝试使用正则表达式查询 type
。这是我正在使用的 MongoDB 查询(并且已被证明可以通过命令行工作):
db.Monkey.find({ "house.type": { "$regex" : ".*coco.*", "$options": "i"}})
我可以使用 Query 中的
对象:filter
方法在 Java 中生成这个精确的字符串
Query query = ...;
query = query.filter("house.type",
Pattern.compile(".*coco.*", Pattern.CASE_INSENSITIVE));
但是,当我尝试在 Java 中运行搜索时,我收到 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.
请注意 house.house.type
的加倍。< /em>
我正在使用 0.99 版本Morphia,并使用 MongoDB Java 驱动程序 2.5 版。我是不是做错了什么?或者这个问题已经在新版本中修复了?
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 from 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个技巧,它对我有用:
Try this trick, it works for me: