Spring data mongodb检索数据:@Document VO返回null



我试图通过spring框架从mongodb检索数据。一开始我设置了返回类型Map<String, Object>,但我决定改为User值对象。

下面是用户VO 的类
@Document(collection = "user")
public class User {
    @Id
    @Field(value="id")
    private String id;
    @Field(value="name")
    private String name;
    @Field(value="password")
    private String password;
    @Field(value="professional")
    private String professional;
    @Field(value="email")
    private String email;
    @Field(value="gravatar")
    private String gravatar;
    @PersistenceConstructor
    public User(String id, String name, String password, String professional, String email, String gravatar) {
        super();
        this.id = id;
        this.name = name;
        this.password = password;
        this.professional = professional;
        this.email = email;
        this.gravatar = gravatar;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getProfessional() {
        return professional;
    }
    public void setProfessional(String professional) {
        this.professional = professional;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getGravatar() {
        return gravatar;
    }
    public void setGravatar(String gravatar) {
        this.gravatar = gravatar;
    }
};

和Here is @repository检索数据

@Repository
public class MongoMemberDao implements CommonDao<String, Map<String, Object>, Exception> {
    @Autowired
    MongoTemplate template;
    final String COLLECTION_NAME = "user";
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> read(String key) throws Exception {
        Query findQuery = new Query();
        findQuery.addCriteria(Criteria.where("id").is(key));
        return template.findOne(findQuery, Map.class, COLLECTION_NAME);
    }
    public User readByDocument(String id) throws Exception {
        Query findOneQuery = new Query();
        findOneQuery.addCriteria(Criteria.where("id").is(id));
        return template.findOne(findOneQuery, User.class, COLLECTION_NAME);
    }
};

read方法返回fine,但是readByDocument不返回(返回null而不是User实例)。我读了官方文件。但是我一点也不明白。

供参考,参数Query看起来是一样的。Query: { "id" : "system"}, Fields: null, Sort: null

  • 我想知道为什么readByDocument返回null

谢谢。

——编辑

Follow is my Database Config

@Configuration
public class MongoConfig extends AbstractMongoConfiguration {
    private final String MONGO_URL = "127.0.0.1";
    private final Integer MONGO_PORT = 27017;
    @Override
    protected String getDatabaseName() {
        return "tfarm";
    }
    @Override
//  @Bean
    public Mongo mongo() throws Exception {
        return new MongoClient(MONGO_URL, MONGO_PORT);
    }
}

我把这个添加到WebApplictaionInitializer实现中

对于当前解决方案

我在官方网站上找到了跟随者

  • 一个带@Id注释的字段(org.springframework.data.annotation.Id)将被映射到_id字段。
  • 没有注释但命名为id的字段将被映射到_id场。
  • 标识符的默认字段名是_id,可以自定义

所以我把我的VO改成…

@Document(collection = "user")
public class User {
    @Id
    private ObjectId _id;
    @Field(value="id")
    private String id;
    @Field(value="name")
    private String name;
    @Field(value="password")
    private String password;
    @Field(value="professional")
    private String professional;
    @Field(value="email")
    private String email;
    @Field(value="gravatar")
    private String gravatar;
    @PersistenceConstructor
    public User(String id, String name, String password, String professional, String email, String gravatar) {
        super();
        this.id = id;
        this.name = name;
        this.password = password;
        this.professional = professional;
        this.email = email;
        this.gravatar = gravatar;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public ObjectId get_id() {
        return _id;
    }
    public void set_id(ObjectId _id) {
        this._id = _id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getProfessional() {
        return professional;
    }
    public void setProfessional(String professional) {
        this.professional = professional;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getGravatar() {
        return gravatar;
    }
    public void setGravatar(String gravatar) {
        this.gravatar = gravatar;
    }
};

增加了ObjectId。或者,只是删除@Id注释也可以。然而

@Id
@Field(value="id")
String id;

不起作用。谢谢你的帮助。

相关内容

  • 没有找到相关文章

最新更新