Friday, 28 December 2012

Retrieve limited field in hibernate

Assume we have hibernate generated POJO like

public class Customermaster implements java.io.Serializable {

private Integer customerId;
private String companyName;
private String contactPersonName;
private String contactNumber;
private String customerWebsite;
private String customerAddress1;
private String customerAddress2;
private String customerEmail;
private String customerZipCode;
private String customerCity;
private String customerState;
private String customerCountry;

        . . .
        .....
        . . .
}

Now we want only customerId, companyName and contactNumber in pojo object instead of Object[] or Map.

For that create constructor of that limited fields in pojo class.
Here we create contructor like

public Customermaster(Integer customerId, String companyName, String       contactNumber ) 
{
this.customerId = customerId;
this.companyName = companyName;
                this.contactNumber  = contactNumber ;
}

And finally we write query in dao like

List<Customermaster> customerList  =  List<Customermaster>)session.createQuery("select new Customermaster(cm.customerId ,cm.companyName, cm.contactNumber ) from Customermaster cm where .... ").list();

and we have list of Customermaster with only three fields, remains are null.

Thursday, 26 July 2012

Retrieve limited field from table AS A MAP in Hibernate



When you are retrieving limited (e.g. 7-8) fields from tables, you need to remember that which field located on which index. Instead of this you can use map in hibernate to come out from sophisticated work.


**********************************************************************************************************************************
Good Approach : Retrieve multiple properties as a list of type Map<String,String>.
Benefit : Just give alias to properties and use that alias as a key in Map.


                List<Map<String,String>> usermasterMapList = null;

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=null;
try
{
session.beginTransaction();
usermasterMapList = session.createQuery("select new map(username as username,password                                                as password,role as role) from Usermaster where isdeleted = 'N'").list();

for(int i=0; i<usermasterMapList.size(); i++) {

Map<String,String> usermasterMap = usermasterMapList.get(i);

System.out.println("Username : " + usermasterMap.get("username"));
System.out.println("Password : " + usermasterMap.get("password"));
System.out.println("Role : " + usermasterMap.get("role"));
}
}
catch (RuntimeException e)
{
if(tx != null)
tx.rollback();
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
if(tx != null)
tx.rollback();
}


**********************************************************************************************************************************

Approach : Retrieve multiple properties as a list of type Object[].
Limitations : Must have to remember indices of particular field. Like obj[0],obj[1],obj[2],... and so on.

List<Object[]> usermasterList = null;

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx=null;
try
{
session.beginTransaction();
usermasterList = session.createQuery("select 
                                                 username,password,role from Usermaster where isdeleted = 'N'").list();


                  for(int i=0; i<usermasterList.size(); i++) {

Object[] obj = usermasterList.get(i);

String username = obj[0].toString();
String password = obj[1].toString();
String role = obj[2].toString();
  
        System.out.println("Username :: " + username);
System.out.println("Password :: " + password);
System.out.println("Role :: " + role);


}
catch (RuntimeException e)
{
if(tx != null)
tx.rollback();
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
if(tx != null)
tx.rollback();
}

**********************************************************************************************************************************