Spring数据Mongo DB检索数据:@Document VO返回null

时间:2016-11-08 02:38:00

标签: java mongodb spring-mvc spring-data value-objects

我正在尝试通过spring框架从mongodb检索数据。 起初我做了返回类型Map<String, Object>,但我决定更改为User值对象。

以下是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;
    }
};

以及@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方法返回正常,但readByDocument不返回(返回null而非User实例)。我看了official document。但我没有得到任何线索。

仅供参考,两者的参数Query看起来相同。 Query: { "id" : "system"}, Fields: null, Sort: null

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

感谢。

----编辑

关注我的数据库配置

@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工具。

1 个答案:

答案 0 :(得分:1)

目前的解决方案

我在官方网站上找到了

  • 使用@Id注释的字段(org.springframework.data.annotation.Id) 将被映射到_id字段。
  • 没有注释但名为id的字段将映射到_id 字段。
  • 标识符的默认字段名称为_id,可以自定义 通过@Field注释。

所以我改变了我的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;

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