【发布时间】:2017-08-24 14:49:46
【问题描述】:
我在使用带有自定义映射器/类型转换器的 Morphia 时遇到问题
鉴于以下 POJO
@Entity("users")
public class User{
private String username = null;
private String password = null;
}
问题是,在给定的 MongoDB(不在我的控制之下)中,值 不是 只是像
{
"email": "xy@test.com",
"password": "abc"
}
但物体看起来更像
{
"usersettings": {
"email": "xy@test.com",
"password": [
"abc", "cde", "efg"
]
}
}
(如您所料,真实世界的 Mongo 文档要复杂得多)
所以我必须将“usersettings.email”映射到我的“username”成员,并将“usersettings.password.0”(仅限第一个数组)映射到我的“password”成员。
我知道 Morphia 中有 TypeConverters,你可以注册它们,但它们只对成员有效,对类无效。
换句话说,这不起作用(它只是在运行时被忽略):
@Entity("users")
@Converters("MyUserConverter.class") <-- this does NOT WORK!
public class User{
private String email = null;
private String password = null;
}
它适用于这样的成员:
@Entity("users")
public class User{
private String email = null;
@Converters("MyCustomTypeConverter.class") <-- this would work, but does not help in my case!
private MyCustomType password = null;
}
问题是,我需要映射整个班级,而不仅仅是某些成员。 我该怎么做?
【问题讨论】:
标签: morphia