Monday, October 18, 2021

Projection in hibernate

 Projection are used to execute aggregate operations and for single value query .

EX -

public static void main(String[] args) {
    SessionFactory factory = new Configuration().configure().addAnnotatedClass(Student.class).buildSessionFactory();
    Session session = factory.getCurrentSession();
    try {
        session.beginTransaction();
        Criteria c = session.createCriteria(Student.class);
        Projection p = Projections.property("lastName");
        List<String> students = c.setProjection(p).list();
        for(String s:students)
            System.out.println(s);
        session.getTransaction().commit();
        session.close();
    } finally {
        factory.close();
    }
}
It will return values of last name column.
Projections are useful for 
1.Fetching only certain column of the table.
2.To execute aggregate function.

No comments:

Post a Comment

Testing controller

------Controller------------- @RestController @RequestMapping("/items") public class ItemController {     private final ItemServic...