Friday, August 12, 2022
HomeSoftware DevelopmentHibernate - Mapping Listing - GeeksforGeeks

Hibernate – Mapping Listing – GeeksforGeeks


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()

                  .configure("hibernate.cfg.xml")

                  .build();

        Metadata meta

            = new MetadataSources(standardServiceRegistry)

                  .getMetadataBuilder()

                  .build();

  

        SessionFactory sessionFactory

            = meta.buildSessionFactory();

        Session session = sessionFactory.openSession();

  

        Transaction transaction

            = session.beginTransaction();

  

        ArrayList<String> investmentList1

            = new ArrayList<String>();

        investmentList1.add("NSC");

        investmentList1.add("PPF");

        investmentList1.add("LIC");

  

        ArrayList<String> investmentList2

            = new ArrayList<String>();

        investmentList2.add("HousingLoan");

        investmentList2.add("CarLoan");

        investmentList2.add("NSC");

  

        EmployeeInvestments employeeInvestment1

            = new EmployeeInvestments();

        employeeInvestment1.setEmployeeName("EmployeeA");

        employeeInvestment1.setInvestments(investmentList1);

  

        EmployeeInvestments employeeInvestment2

            = new EmployeeInvestments();

        employeeInvestment2.setEmployeeName("EmployeeB");

        employeeInvestment2.setInvestments(investmentList2);

  

        session.persist(employeeInvestment1);

        session.persist(employeeInvestment2);

  

        transaction.commit();

        session.close();

        System.out.println(

            "success. We have seen List mapping here. Check the db data. We can see the index maintained there");

    }

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments