Chapter 4. Persisting Collections
Working with data structures, formally known as Java collections, is inevitable for any Java developer. In this chapter, we will learn about Hibernate’s support for persisting and retrieving collections. We will look in detail at how we can persist the standard Java collections using the Hibernate framework.
As a Java developer, at some point in your programming life, you have probably worked with Java collections. Java collections are the most common data structures in Java, with various algorithm features.
We will, at some point, encounter persistent objects with collections as their values. Persisting simple values from Hibernate’s view is different from persisting collection elements such as java.util.List or java.util.Map structures. We need to follow certain mapping procedures and processes to let Hibernate know our intentions.
The familiar Java collections—such as List, Set, Array, and Map—are all supported by Hibernate. In fact, it goes one step further and creates a couple of other collections, such as bag and idbag. Let’s look at them one by one.
Designing to Interfaces
Java provides collection interfaces such as java.util.Collection (which is the parent to all the collection interfaces except the map interface), java.util.List (the interface for list data structures), java.util.Set (for set data structures), java.util.Map (for key/value map data structures), and more.
The List implementation is intended to hold ordered lists of elements. We ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access