Hi Friends,
Greetings!
Here Are Some Examples Of Criteris And Projection Queries. HopeFully Al These Going to be helpful for you.
SqlQuery1===>select * from month;
After Coverting:----
List crit= session.createCriteria(Honey1.class).list();
System.out.println("The Count is " +crit);
Ans:-- All Values of Tables;
===============================================
SqlQuery2==> select name from month;
List crit=session.createCriteria(Honey1.class) . setProjection(Projections.property("name")) .list();
System.out.println("The Count is " +crit);
Ans:-- All values of cloumn "name" of Table;
===============================================
SqlQuery3==> select name from month where name='c1';
After Coverting:----
List crit=session.createCriteria(Honey1.class) . setProjection(Projections.property("name")) .add(Restrictions.eq("name","c1")) .list();
System.out.println("The Count is " +crit);
Ans:-- c1
===============================================
SqlQuery 4:- select name,m1,m2,m3 from month
After Coverting:----
List crit = session.createCriteria(Honey1.class). setProjection(Projections.projectionList(). add(Projections.property("name")). add(Projections.property("m1")). add(Projections.property("m2")). add(Projections.property("m3")) ).list();
Iterator iter = crit.iterator();
if (!iter.hasNext())
{
System.out.println("No objects to display.");
return;
}
while (iter.hasNext())
{
System.out.println("New object");
Object[] obj = (Object[]) iter.next();
for (int i=0;i<obj.length;i++)
{
System.out.println(obj[i]);
}
}
Ans:-New object
a1
am1
am2
am3
New object
b1
bm1
bm2
bm3
New object
c1
cm1
cm2
cm3
New object
d1
dm1
dm2
dm3
==============================================
SqlQuery5:-- select name m1,m2,m3 from month where name='c1';
After Coverting:----
List crit = session.createCriteria(Honey1.class). add(Restrictions.eq("name", "c1")). setProjection(Projections.projectionList(). add(Projections.property("name")). add(Projections.property("m1")). add(Projections.property("m2")). add(Projections.property("m3")) ).list();
Iterator iter = crit.iterator();
if (!iter.hasNext())
{
System.out.println("No objects to display.");
return;
}
while (iter.hasNext())
{
System.out.println("New object");
Object[] obj = (Object[]) iter.next();
for (int i=0;i<obj.length;i++)
{
System.out.println(obj[i]);
}
}
Ans:-New object
c1
cm1
cm2
cm3
===============================================
These Are The Most Common Queries Which Can Be Used Instaed Of Using HQL. And It Increases The Speed Of Ur Apllication Than HQL.
In Next Post i Will Be Sending Little Complicated Queries By Using Criteria And Projections.
I fMy Post Is Helpful For You Guys Then Plz DOnt Forget To Rate..
Thanks Alot..
With High Regards
Saurav Jain
_________________ I Really want to share my knowledge and learn from you all people specaily about Hibernate.. Thanks Lot
|