• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to retrieve only perticular attributes from table

 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bean:

User
{
int userId;
String userName;
String password;
Date passwordExpireDate;
}

There is a respective table for this bean in database.

Now I want to retrieve List<User> which contains only userId and userName

I dont want manual solution.

Does hibernate provide such facility (Just set such criteria and retrieve bean with only required attributes)?
Please help me if anybody has tried this without manual fetch and convert.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate will return fully populated objects.

However, you can do a SQL query and just get an array back with just the fields you want. But really, you'll get fully populated objects back from Hibernate, not objects with certain fields populated and others not.

Read a similar post here:
JavaRanch Post on Returning Instances vs Arrays


-Cameron McKenzie
 
Benjamin Winterberg
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its possible to create new Objects via HQL:

 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Solution provided by Ben is interesting I would like to try it

For time being I have retrieved required beans using simple query..

I will surely update the thread once I successfully execute it...

Thanks everyone for quick responses

sorry for late response
 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Successfully tried Ben's Way...

Thank you very much Ben



This is how I tried...
One important thing Ben forgot to mention is, It is necessary to have a constructor of specified no of attribute you want to retrieve.

eg If you want to fetch bean that contains only userId, It is necessary to have constructor User(Long userId) in your bean.


Thank you all,
Cheers...
 
Benjamin Winterberg
Ranch Hand
Posts: 36
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome, Ben! I hadn't even thought of that!

-Cameron McKenzie
 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is this a valid use?

SQLQuery q = session.createSQLQuery("select new Office(o.getTransactionID(), o.getOfficeName()) from Office o");
 
Benjamin Winterberg
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhavesh Dak wrote:
Is this a valid use?

SQLQuery q = session.createSQLQuery("select new Office(o.getTransactionID(), o.getOfficeName()) from Office o");


No, its not valid. The query string looks like HQL, so you should create a HQL Query via session.createQuery(...).
 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Got it....

Actually I forgot to remove SQL word out of statement.


I was trying some thing like this...


But I found that HQL internally calls getters and setters of the bean.

So finally,

worked fine for me


One more thing I would Like to know is, Is it possible that we can manipulate java code inside query ?


 
Benjamin Winterberg
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhavesh Dak wrote:One more thing I would Like to know is, Is it possible that we can manipulate java code inside query ?

What do you mean exactly by manipulating java code? There are many ways to manipulate java code programmatically (e.g. JDK Proxies, Bytecode Instrumentation etc) but I dont think its possible to do something like that via HQL.
 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing I would like to know is:

Suppose I have following class design

X { id , name} //(abstract Class)

Y extends X
Z extends X

Now I have one Client Class which is an aggregation of type class X.

Client {long id, String name, X x}

e.g. now I want to execute query, (Assuming all the required constructors available)



Will this work or how can I achieve this?
 
Benjamin Winterberg
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhavesh Dak wrote:e.g. now I want to execute query, (Assuming all the required constructors available)



Will this work or how can I achieve this?


Dont know if it works. Just try it out. If its not working, you can easily work around by creating the objects manually:

 
John Bengler
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could move the



to your Constructor...




But I suppose you want to have different queries and create different objects in these queries...


John
 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much ben and John...

This stuff is really helpful to me.

I will try this out in production and update soon as I come up with anything notifiable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic