-->
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.  [ 4 posts ] 
Author Message
 Post subject: createCriteria question
PostPosted: Wed Feb 15, 2006 2:59 pm 
Beginner
Beginner

Joined: Wed Feb 15, 2006 2:51 pm
Posts: 20
Hello,


What I am trying to do is select 2 fields from a table that has 8 fields.

Using the createQuery this is easy
session.createQuery("Select lname,fname from Person");
I get back an object and then I can select last name and first name using the first and second array

This is great because it creates a query for me that only select the 2 fields from the database (saving some resources)

Object[] row = (Object[]) iter.next();
System.out.println("First Name: " + row[0]);
System.out.println("Last Name: " + row[1]);



How would I do the same thing using session.createCriteria(Person.class)
I just want to select the first name and the last name from person.




Thank you








Hibernate version:3


Top
 Profile  
 
 Post subject: Re: createCriteria question
PostPosted: Wed Feb 15, 2006 3:11 pm 
Newbie

Joined: Wed Feb 15, 2006 2:25 pm
Posts: 2
i think this should work.

Code:
List results = session.createCriteria(Domestic.class, "Person")
    .setProjection( Projections.projectionList()
        .add( Projections.property("person.lname"), "lastName" )
        .add( Projections.property("person.fname"), "firstName" )
    )
    .list();


Top
 Profile  
 
 Post subject: Re:createCriteria question
PostPosted: Wed Feb 15, 2006 3:22 pm 
Newbie

Joined: Wed Feb 15, 2006 9:22 am
Posts: 7
Location: Florham Park, NJ
Golly97;

Define all but the two properties of the Person class as lazy="true". Something like this:

Code:
<class name="Person" ........

        <property name="firstName" lazy="false" ....
        </property>

        <property name="lastName" lazy="false" ....
        </property>

        <property name="lazyField1" lazy="true" ....
        </property>

        <property name="lazyField2" lazy="true" ....
        </property>

        .
        .
        .
        .

        <property name="lazyField6" lazy="true" ....
        </property>

</class>


The your criteria query would look like this:

Code:
for(Iterator iter = session.createCriteria(Person.class).list().iterator(); iter.next();) {
   Person person = (Person)iter.next();
   System.out.println("First Name: " + person.getFirstName());
   System.out.println("Last Name: " + peson.getLastName());
}



//Nicholas


Top
 Profile  
 
 Post subject: Re: createCriteria question
PostPosted: Wed Feb 15, 2006 4:02 pm 
Beginner
Beginner

Joined: Wed Feb 15, 2006 2:51 pm
Posts: 20
hubkabel wrote:
i think this should work.

Code:
List results = session.createCriteria(Domestic.class, "Person")
    .setProjection( Projections.projectionList()
        .add( Projections.property("person.lname"), "lastName" )
        .add( Projections.property("person.fname"), "firstName" )
    )
    .list();



thank you hubkabel
this works


NickMan,
this works, but the query fetched all the rows from the database, before i only displayed first and last name.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.