Spring数据MongoDB存储库和继承 [英] Spring-data mongodb repository and inheritance
问题描述
我已经创建了一个这样的类用户类:
I have created a class user class like that :
public class User {
/**
* The list of roles for the user
*/
private List<String> roles;
/**
* The login information
*/
private ICredentials credentials;
/*---- the constructors and getters/setters are omitted ---*/
}
ICredentials接口:
The ICredentials interface :
public interface ICredentials {
}
一个实现:
public class LoginPassword implements ICredentials {
/**
* The login of the user
*/
protected String login;
/**
* The password of the user
*/
protected String password;
/*---- the constructors and getters/setters are omitted ---*/
}
现在,我已经创建了一个存储库,以从凭据中查找用户:
Now, I have created a repository to find a user from credentials :
public interface MongoUserRepository extends MongoRepository<User, String> {
List<User> findByCredentials(ICredentials credentials);
List<User> findByCredentialsEquals(ICredentials credentials);
}
Spring对此记录(对于两个请求):
org.springframework.data.mongodb.core.MongoTemplate [DEBUG]使用查询查找:{ "credentials" : { "login" : "test" , "password" : "test"}}
字段:类:xxx类的null.集合中的用户:user
Spring logs this (for both requests) :
org.springframework.data.mongodb.core.MongoTemplate [DEBUG] find using query: { "credentials" : { "login" : "test" , "password" : "test"}}
fields: null for class: class xxx.User in collection: user
它什么也没找到……似乎什么也没找到,因为请求未写入"_class"属性.我认为好的要求应该是:
{ "credentials" : {
"_class":"xxx.LoginPassword"
, "login" : "test" , "password" : "test"}}
And it doesn't found anything... It seems that it doesn't found anything because the "_class" attribute is not written is the request. I think the good request should be:
{ "credentials" : {
"_class":"xxx.LoginPassword"
, "login" : "test" , "password" : "test"}}
我做错了什么吗?我想念什么吗?
Is something I doing wrong ? Am I missing something ?
谢谢
我使用spring mvc 3.2.0,spring-data-commons-core 1.4.0,spring-data-mongodb 1.1.1和mongo-java-driver 2.10.1(我已将所有库更新为最新版本确保它不是已经修复但没有成功的错误)
I use spring mvc 3.2.0, spring-data-commons-core 1.4.0, spring-data-mongodb 1.1.1 and mongo-java-driver 2.10.1 (I have updated all libraries to the latest version to be sure it wasn't a bug already fixed but no success)
推荐答案
您尝试在ICredentials界面上方添加@Document(collection ="users")吗?
Did you try to add @Document(collection = "users") above your ICredentials interface ?
这篇关于Spring数据MongoDB存储库和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!