-->
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.  [ 3 posts ] 
Author Message
 Post subject: selecting only few columns from entity
PostPosted: Thu Jun 29, 2006 4:47 am 
Newbie

Joined: Thu Jun 29, 2006 3:51 am
Posts: 7
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1

Hi,
I am new to hibernate.
I am wondering if is it possible in Hibernate to select only few columns from the entity.
Normally when I create query using following code :

Query query = session.session.createQuery("from " + entityName );

above query will fetch all the columns mapped in the Entity.

But I want my query to fetch only two column out of 6 columns Entity it has.

I can't change my entity because it's being used by other application too..

Please help
Thanks in advance.


Top
 Profile  
 
 Post subject: Re: selecting only few columns from entity
PostPosted: Thu Jun 29, 2006 6:04 am 
Newbie

Joined: Fri Apr 21, 2006 10:35 am
Posts: 13
Location: de
Of course it's possible. You can use a SELECT clause:
Code:
Query query = session.createQuery( "select firstname, lastname from Person" )

That gives you the first and last names of all persons, but not the persons' other attributes. When you call query.list(), you'll get a list of array objects, which you can iterate like this:
Code:
List results = query.list();
for (Iterator it = results.iterator(); it.hasNext(); ) {
   Object[] myResult = (Object[]) it.next();
   String firstName = (String) myResult[0];
   String lastName = (String) myResult[1];
   System.out.println( "Found " + firstName + " " + lastName );
}


You can even create objects like that. If you have a class Name with a two-parameter constructor for first name and last name, you can reformulate your query to get a list of Name objects rather than Object[]:
Code:
Query query = session.createQuery( "select new Name(firstName, lastName) from Person" )


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 6:32 am 
Newbie

Joined: Thu Jun 29, 2006 3:51 am
Posts: 7
Thanks Yuunli ,

It helped.
I have one query regarding second option.. Once I 'll get Person objects.. will it also use Entity cache.. as it does in normal scenario.

regards,
Prasoon


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