79562624

Date: 2025-04-08 16:51:27
Score: 1
Natty:
Report link

@Reference (or its more current equivalent @DBRef) tells Spring Data to store a reference (like an ObjectId) to another document, not to embed it.

You must save Z documents first:

Z zObj = zRepository.save(new Z(...)); // save first
Grp grp = new Grp();
grp.setX("someValue");
grp.setY(List.of(zObj));
grpRepository.save(grp); // now it will store the DBRef

or

You've to embed Z directly inside grp

Then don’t use @Reference — just declare it as a regular field:

private List<Z> y;
Reasons:
  • Has code block (-0.5):
  • User mentioned (1): @Reference
  • User mentioned (0): @DBRef
  • User mentioned (0): @Reference
  • Low reputation (0.5):
Posted by: Vijay