Open In App

Hibernate – Mapping List

Last Updated : 10 Jun, 2022
Like Article

In Hibernate, in order to go for an ordered collection of items, mostly List is the preferred one, Along with List, we have different collection mapping like Bag, Set, Map, SortedSet, SortedMap, etc., But still in many places mapping list is the most preferred way as it has the index element and hence searching wise, doing any CRUD operation wise it is easier. We can see this here via employees and their saving plans. One employee can do multiple investments. we can see that here.

Example Project

Project Structure:

Project Structure


This is a maven-driven project.



        <!-- As we are connecting with MySQL, this is needed -->

Let us see the main configuration files



<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
          "-//Hibernate/Hibernate Mapping DTD 5.3//EN"  
 <class name="com.gfg.hibernate.pojo.EmployeeInvestments" table="Employee">  
   <id name="employeeId">  
     <generator class="increment"></generator>  
   <property name="employeeName"></property>  
   <!--  In list, we need to maintain the order. Hence we 
         should maintain the index element. 
            This is the major difference between bag and list. -->
  <list name="investments" table="Investments">  
  <!--  Foreign key connecting employee and investments -->
  <key column="employeeId"></key>  
  <!-- This will maintain the order of investments, starts with 0 
       Because of this, it is easier to search and do crud operation -->
  <index column="type"></index>  
  <element column="investment" type="string"></element></list>               



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
      <!--  As we are connecting mysql, those driver classes, 
            database name, username and password are specified
            Please change the information as per your requirement -->
        <property name="">update</property>  
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/geeksforgeeks?serverTimezone=UTC</property>        
        <property name="connection.username">root</property>
        <property name="connection.password">admin</property>
        <property name="show_sql">true</property>
        <mapping resource="employeeinvestment.hbm.xml" /> 

Let us see the POJO class now


import java.util.List;
public class EmployeeInvestments {
    // data member for employee
    private int employeeId;
    private String employeeName;
    // investments
    private List<String> investments;
    public int getEmployeeId() { return employeeId; }
    public void setEmployeeId(int employeeId)
        this.employeeId = employeeId;
    public String getEmployeeName() { return employeeName; }
    public void setEmployeeName(String employeeName)
        this.employeeName = employeeName;
    public List<String> getInvestments()
        return investments;
    public void setInvestments(List<String> investments)
    { = investments;

Java file for adding and listing the data


import com.gfg.hibernate.pojo.EmployeeInvestments;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class EmployeeInvestmentRepository {
    public static void main(String[] args)
        StandardServiceRegistry standardServiceRegistry
            = new StandardServiceRegistryBuilder()
        Metadata meta
            = new MetadataSources(standardServiceRegistry)
        SessionFactory sessionFactory
            = meta.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction
            = session.beginTransaction();
        ArrayList<String> investmentList1
            = new ArrayList<String>();
        ArrayList<String> investmentList2
            = new ArrayList<String>();
        EmployeeInvestments employeeInvestment1
            = new EmployeeInvestments();
        EmployeeInvestments employeeInvestment2
            = new EmployeeInvestments();
            "success. We have seen List mapping here. Check the db data. We can see the index maintained there");

On running the project, we can see the output below.




Let us see the query that got executed



Let us check MySQL DB data




In the above, we have seen how to use the mapping list in the hibernate. Wherever ordering is necessary, we can go for List mapping.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads