【问题标题】:Spring-data mongodb repository and inheritanceSpring-data MongoDB 存储库和继承
【发布时间】:2013-01-11 18:26:51
【问题描述】:

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

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(我已将所有库更新到最新版本确保它不是已经修复但没有成功的错误)

【问题讨论】:

    标签: spring mongodb spring-data-mongodb


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      @Document(collection = "example")
      public class Example{
      
        @Id
        private String id;
      
        //getter and setters
      }
      

      【讨论】:

        猜你喜欢
        • 2017-09-07
        • 2012-08-16
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        • 2013-05-18
        • 2012-05-06
        • 1970-01-01
        • 2017-06-16
        相关资源
        最近更新 更多