< Prev
Next >

Hibernate Mapping Sorted Map - TreeMap

Today we are going to understand how to persist a collection object based on a sorted map(TreeMap) collection within the database using Hibernate.

By default, TreeMap stores key-value pairs in a sorted ascending order(based on the key), hence Hibernate will store the elements mapped from a TreeMap in an ascending order but the retrieved collection of the same elements will not be in any order, because Hibernate uses the functionality of Map interface when a Map of any kind(sorted - TreeMap or unsorted - HashMap)is pulled out of database. hence we will still have to implement the Comparator interface.

Why do we have to add a collection object in an Entity class.

Let's suppose we have a class Companies containing the information such as - id, name and a Set collection of String object(representing the first name and last name of all the employees of a particular company). That's why we are going to need a collection object in Companies class, representing multiple employee names in a company.

Let's put this example to work and make it easier for you to understand the whole concept.

Note :

Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition