Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Hibernate Persist a List Object in Hibernate

Have Programming Question? Ask it here!
 
 
 


Deepak Lal
Persist a List Object in Hibernate
1 Answer(s)      5 years and 10 months ago
Posted in : Hibernate



Advertisement
View Answers

November 10, 2008 at 7:07 AM


Hi friend,


package net.roseindia;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;

public class projectionExample {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Session sess = null;
try{
SessionFactory sfact = new Configuration().configure().buildSessionFactory();
sess = sfact.openSession();
Criteria crit = sess.createCriteria(Product.class);
ProjectionList proList = Projections.projectionList();
proList.add(Projections.property("name"));
proList.add(Projections.property("price"));
crit.setProjection(proList);
List list = crit.list();
Iterator it = list.iterator();
if(!it.hasNext()){
System.out.println("No any data!");
}
else{
while(it.hasNext()){
Object[] row = (Object[])it.next();
for(int i = 0; i < row.length;i++){
System.out.print(row[i]);
System.out.println();
}
}
}
sess.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}

}

}


---------------------------------------

Visit for more information.

http://www.roseindia.net/hibernate/projections/hibernate-projections.shtml

Thanks


Related Tutorials/Questions & Answers:
 

 

 

DMCA.com