使用Spring Data Mongo时@DBRef不会提取数据 [英] @DBRef doesn't pull the Data when use Spring Data Mongo

查看:239
本文介绍了使用Spring Data Mongo时@DBRef不会提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下代码:错误创建名称为'personRepository'的bean:调用init方法失败;嵌套异常是com.mongodb.util.JSONParseException:,现在尝试调用

I used the code from : Error creating bean with name 'personRepository': Invocation of init method failed; nested exception is com.mongodb.util.JSONParseException: and now trying to call

Person p = personRepository.findByAddresses_City("London NW1");
System.out.println("PERSON FOR ADDRESS = "+p);

我得到的响应为空.

此外,下面的查询不会提取任何内容.

Also, below query doesn't pull anything.

Query query = new Query(Criteria.where("address.$id").is(2L));
List<Person> l = mongoTemplate.find(query, Person.class);
System.out.println(l);

这里:

db.getCollection('地址').find({})

db.getCollection('address').find({})

/* 1 */
{
    "_id" : NumberLong(1),
    "address" : "221b Baker Street",
    "city" : "London NW1",
    "state" : "London",
    "zipcode" : NumberLong(12345),
    "_class" : "com.example.demo.model.Address"
}

db.getCollection('person').find({})

/* 1 */
{
    "_id" : NumberLong(1),
    "name" : "Achilles",
    "age" : 0,
    "addresses" : [],
    "_class" : "com.example.demo.model.Person"
}

/* 2 */
{
    "_id" : NumberLong(2),
    "name" : "Hektor",
    "age" : 0,
    "addresses" : [ 
        {
            "$ref" : "address",
            "$id" : NumberLong(1),
            "$db" : "address"
        }
    ],
    "_class" : "com.example.demo.model.Person"
}

如何解决此错误?

@Document(collection = "address")
public class Address {

    @Id
    private long addressId;
    private String address;
    private String city;
    private String state;
    private long zipcode;

    public Address() {
        System.out.println("CAlling default cons");
    }

    @PersistenceConstructor
    public Address(long addressId, String address, String city, String state, long zipcode) {
        this.addressId = addressId;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zipcode = zipcode;
    }
    // setter and getter... toString()..
}

@Document
public class Person {
    @Id
    private Long personId;

    private String name;

    private int age;

    @DBRef(db = "address")
    private List<Address> addresses = new ArrayList<>();

    public Person() {
    }

    @PersistenceConstructor
    public Person(Long personId, String name, int age) {
        super();
        this.personId = personId;
        this.name = name;
        this.age = age;
    }
    // setter, getter and toString
}

推荐答案

这按设计工作. MongoDB不允许通过查询进行应用程序级联接,您需要对更复杂的查询使用聚合框架.因此,存储库查询仅允许按完整值(即Address对象)或标识符查找DBRef.

This works as designed. MongoDB does not allow application level joins via queries, you'd need to use the aggregation framework for more complex queries. Thus, repository queries only allow to find DBRefs by complete value (i.e Address objects) or identifiers.

如果将where子句固定为address.addressId,则第二个示例应该可以工作.

The second example should work if you you fix the where clause to address.addressId.

P.S .:请避免仅仅因为您在这里没有立即得到答案而提交票证.如果您要提交票证,请确保在示例项目中附上测试用例.

P.S.: Please avoid filing tickets just because you don't immediately get an answer here. If you file a ticket, please be sure to attach a sample project with a test case.

这篇关于使用Spring Data Mongo时@DBRef不会提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆