2

我创建了一个这样的类用户类:

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 接口:

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 ---*/
}

现在,我创建了一个存储库来从凭据中查找用户:

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] 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"}}

我做错了什么吗?我错过了什么吗?

谢谢

我使用 spring mvc 3.2.0、spring-data-commons-core 1.4.0、spring-data-mongodb 1.1.1 和 mongo-java-driver 2.10.1(我已将所有库更新到最新版本以确保它不是已经修复的错误但没有成功)

4

2 回答 2

2

您是否尝试在 ICredentials 界面上方添加 @Document(collection = "users") ?

于 2013-02-20T23:25:51.397 回答
1

当您不为文档使用注释时会发生这种情况

@Document(collection = "example")
public class Example{

  @Id
  private String id;

  //getter and setters
}
于 2014-02-19T21:21:12.607 回答