intermediate~2h

Collections with Spring Boot

How collections show up across a real Spring Boot application — REST controller serialization, JPA entity collection mappings, and the specific pitfalls (mutable getters, uninitialized fields, lazy-loaded collections) that look fine in a code review and break in production.

Learning objectives

  • Explain how List, Set, and Map fields serialize to JSON through Jackson in a Spring Boot controller
  • Choose between List and Set for @OneToMany/@ManyToMany JPA mappings and explain the Cartesian product problem
  • Identify the mutable-getter pitfall where a collection field leaks a live reference out of an entity or DTO
  • Explain why an uninitialized collection field throws NullPointerException instead of behaving like an empty collection
  • Describe how lazy-loaded JPA collections cause the N+1 query problem and how to recognize it in logs

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Core Java Mastery library.

Collections with Spring Boot

Next Step

Continue to Collections: Mistakes That Pass Code Review But Fail in Production →← Back to all Java Collections Framework chapters