0
@Document("scenarios")
public class Scenario {
    @Id
    private String id;
    @Indexed(unique = true)
    private String name;
    private String description;
    @JsonSerialize(using = PlatformSerializer.class)
    private final List<Platform> platforms;
// getters and setters not shown
}

public class Platform {
    private final PlatformData platformData;
    @JsonSerialize(using = RowSerializer.class)
    private final List<Row> rows;
// getters and setters not shown
}

@Repository
public interface ScenarioRepository extends MongoRepository<Scenario, String> {
    Scenario findByName(String name);
}

The scenarios collection in the database contains embedded Platform objects in each document. And the Platform objects contain embedded Row objects. I can successfully get the Scenario object from the database using the findByName method in ScenarioRepository. The name and description values are correct. However, the platforms List is empty. I seem to recall that containers require an annotation in the POJO. Though the Scenario object was correctly saved to the database using this same code. What am I missing? Thanks!

1 Answer 1

0

The cause is:
platforms (and rows) are declared final. When Spring‑Data reads the document it:

  1. calls the no‑arg constructor (which leaves the list empty), then

  2. tries to set each field via reflection – but it skips final fields, so the list stays empty.

Saving still works, because writing only serializes the existing object graph.

// Make the field non‑final 
private List<Platform> platforms

No extra Mongo annotations are needed for an embedded list.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.