Projections in Hibernate

Program 1

package beans;

public class Employee 
{
    private int eid;
    private String ename;
    private String edept;
    private int ehra;
    private int eta;
    private int eda;

    public Employee() {
    }

    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getEdept() {
        return edept;
    }

    public void setEdept(String edept) {
        this.edept = edept;
    }

    public int getEhra() {
        return ehra;
    }

    public void setEhra(int ehra) {
        this.ehra = ehra;
    }

    public int getEta() {
        return eta;
    }

    public void setEta(int eta) {
        this.eta = eta;
    }

    public int getEda() {
        return eda;
    }

    public void setEda(int eda) {
        this.eda = eda;
    }
    
    
}

Program 2

<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="beans.Employee" table="emp" schema="dataflair">
        <id name="eid" column="empid"></id>
        <property name="ename" column="empname"></property>
        <property name="edept" column="dept"></property>
        <property name="ehra" column="hra"></property>
        <property name="eta" column="ta"></property>
        <property name="eda" column="da"></property>
    </class>
</hibernate-mapping>

Program 3

<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
        <property name="connection.url">jdbc:mysql://localhost/dataflair</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root@data</property>
        <property name="connection.pool_size">10</property>
         <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <mapping resource="resources/employee.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Program 4

package view;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import beans.Employee;
import java.util.*;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
public class TestMain 
{
    public static void main(String[] args) 
    {
        Configuration cfg=new Configuration();
        cfg.configure("resources/hibernate.cfg.xml");
        SessionFactory sf=cfg.buildSessionFactory();
        Session session=sf.openSession();
        Criteria C=session.createCriteria(Employee.class);   // Select * from Employee
        Projection P1=Projections.property("ename");
       // Projection P2=Projections.property("edept");
        C.setProjection(P1);
       // C.setProjection(P2);
        List<String>myname=C.list();
        for(String S:myname)
        {
            System.out.println(S);
        }
        
//        for(Employee E:mylist)
//        {
//            System.out.print("  "+E.getEid());
//            System.out.print("  "+E.getEname());
//            System.out.print("  "+E.getEdept());
//            System.out.print("  "+E.getEhra());
//            System.out.print("  "+E.getEta());
//            System.out.println("  "+E.getEda());
//        }
        session.close();
        sf.close();
    }
    
}

Program 5

package view;

import beans.Employee;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import java.util.*;
public class TestMain1 
{
    public static void main(String[] args) 
    {
        Configuration cfg=new Configuration();
        cfg.configure("resources/hibernate.cfg.xml");
        SessionFactory sf=cfg.buildSessionFactory();
        Session session=sf.openSession();
        Criteria C=session.createCriteria(Employee.class);   // Select * from Employee
        Projection P1=Projections.property("ename");
        Projection P2=Projections.property("edept");
        ProjectionList plist=Projections.projectionList();
        plist.add(P1);
        plist.add(P2);
        C.setProjection(plist);
        List<Object[]>mylist=C.list();
        for(Object row[]:mylist)
        {
            System.out.print("  "+row[0]);
            System.out.println("  "+row[1]);
        }
        session.close();
        sf.close();
    }
    
}

 

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *