2

I am using mongodb with spring and I added spring-data-mongodb using maven

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.4.0.RELEASE</version>
    </dependency>

I am trying to use @DBRef lazy load but I get error

@DBRef(lazy = true)
private List<Bill> shoppingHistory = new ArrayList<>();

my error is

The attribute lazy is undefined for the annotation type DBRef

I can only user db attribute.

Can somebody help me how to solve this problem?

Mišel Ademi
  • 187
  • 4
  • 15
  • 2
    The best way to resolve the problem is to not use `DBRef` at all. It is an old "proposed" solution to representing links to other documents in either other collections or other databases. The problem is that does work very well at all and actually has no support at all in newer features introduced since the initial release of the spec. You are far better off using ["manual references"](https://docs.mongodb.com/manual/reference/database-references/#manual-references) with plain `ObjectId` values, and actually defining the "relationships" in your "application model" instead. – Neil Lunn Jul 08 '17 at 01:24
  • "Joins" can now be accommodated by [`$lookup`](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/) in modern releases of MongoDB. And "lazy" is best handled by issuing another query to the related data as the request needs it instead. Note that `DBRef` is not supported at all by modern operations such as `$lookup` other related actions, nor are there any plans to include such support. – Neil Lunn Jul 08 '17 at 01:30
  • So in truth `DBRef` is as "deprecated" as you get, without an actual "official" deprecation notice. It's not used or considered in modern operations, and I would think likely to be officially deprecated at some point in the future anyway. – Neil Lunn Jul 08 '17 at 01:30

0 Answers0