tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Hibernate > Criteria Queries > Entity Map

Entity Map 

The following example shows creating an entity map from a projection list. Refer first example for the configuration and mapping.

File Name  :  
com/bethecoder/tutorials/hibernate/basic/criteria/CriteriaMapTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.hibernate.basic.criteria;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.transform.Transformers;

import com.bethecoder.tutorials.hibernate.basic.Company;
import com.bethecoder.tutorials.hibernate.basic.util.HibernateUtil;

public class CriteriaMapTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    
    Criteria criteria = session.createCriteria(Company.class);
    criteria.setProjection(Projections.projectionList()
            .add(Property.forName("id").as("company_id"))
            .add(Property.forName("name").as("company_name"))
      );
    criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
    List<?> rows = criteria.list();
    
    for (Object row : rows) {
      System.out.println(row);
    }
    
    session.getTransaction().commit();
    session.close();  
  }
  
}
   

It gives the following output,
{company_id=1, company_name=PQR}
{company_id=2, company_name=ABC}
{company_id=3, company_name=MNO}
{company_id=4, company_name=IJK}
{company_id=5, company_name=ART}
{company_id=6, company_name=RNK}
{company_id=7, company_name=AOQ}
{company_id=8, company_name=BQO}
{company_id=9, company_name=CFR}
{company_id=10, company_name=CFR}


                    Print

   



Books Recommendation

 
 
bl  br