0

Context: Java8 application, Spring MongoDB 1.9.1

I've got a collection containing entities of A, and another collection containing entities of B. In my domain model, Entity A has a collection of B as DBRef like this

class A {
    @DBRef
    List<B> myBs;
}

Moreover, I have registered two AbstractMongoEventListeners, one for A and another one for B. If I fetch a document of type A or B directly via MongoTemplate.findById(), the onAfterConvert() method is triggered alright. However, if I just fetch A, I'd expect that the onAfterConvert method is triggered also for each item B in the DBRef'ed list of A. Actually, it is not.

I hazard a guess that this works as designed, on the other side I don't the see the reason why onAfterConvert shouldn't work for any entity/document being loaded from the DB.

Long story short, I need to do some post processing for all items of B after A has been loaded. Does anyone have an elegant solution for this?

Jan B.
  • 6,030
  • 5
  • 32
  • 53

2 Answers2

1

No clue, if there are better ways to solve this, but this works for me:

Overwrite the resolveDbRef() method in an extended DefaultDbRefResolver and pass it your own MappingMongoConverter when creating the bean.

Other answers/recommendations welcome

Jan B.
  • 6,030
  • 5
  • 32
  • 53
0

I think this is related to https://jira.spring.io/browse/DATAMONGO-1044 - should be fixed in next major Spring Data MongoDB release (1.10).

  • This is more appropriate as a comment. – Sid Dec 29 '16 at 14:17
  • @Sid I could not post a comment. – user2881690 Dec 29 '16 at 15:16
  • @Joe Taras: further information is in the linked issue - I just found it in the Spring Data MongoDB Jira (as I had a similar problem) and thought it might be useful to know that it is being fixed (in fact working in the Ingalls M1 version). – user2881690 Dec 29 '16 at 15:22