-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Imortant Criteria Projection Queries For U all:- Part 1
PostPosted: Fri Apr 27, 2007 1:44 am 
Beginner
Beginner

Joined: Thu Apr 12, 2007 12:04 pm
Posts: 25
Location: Chennai
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 27, 2007 2:56 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi Saurav ,

I appreciate you help to us .I is really very good Example That would really help other guys. Keep it up.

_________________
Dharmendra Pandey


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.