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.  [ 5 posts ] 
Author Message
 Post subject: How to select only few fields
PostPosted: Wed Apr 16, 2008 4:26 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
Hi,

I have a user object, with name, loginid, password, zip and e-mail. When I like to validate the password, I would like User object to be retrived given the login id. However instead of getting the values of all fields of user object, I only want loginid and password of the user object to be selected rather than all other fields.

Is there a way to build such query.. i.e partially filled java objects.. for optimisation purpose.

Regards,
Nagendra

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


Top
 Profile  
 
 Post subject: Re: How to select only few fields
PostPosted: Wed Apr 16, 2008 5:16 am 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
nagkumar wrote:
Hi,

I have a user object, with name, loginid, password, zip and e-mail. When I like to validate the password, I would like User object to be retrived given the login id. However instead of getting the values of all fields of user object, I only want loginid and password of the user object to be selected rather than all other fields.

Is there a way to build such query.. i.e partially filled java objects.. for optimisation purpose.

Regards,
Nagendra



Hi,

You can use projection hql to achieve what you need. So, for instance, instead of executing:
“from User user where user.loginid=:loginid”
which will return a (possibly) single User object, you can use projection by executing:
“select user.loginid, user.password from User user where user.loginid=:loginid”
This will return a list (unique in your case for unique login ids) of object arrays just like in normal jdbc and you can use every array by casting to the appropriate class.

A more elegant and object oriented way, however, of manipulating the result set is to use dynamic instantiation. This is done by defining a transient and not mapped class encapsulating the fields you need. So, in your case you would need to create a PartialUser class holding loginid and password (with a related constructor also defined ) and use it like : “select new PartialUser(user.loginid, user.password) from User user where user.loginid=:loginid”.


Hope that helps,


mosa


Top
 Profile  
 
 Post subject: Re: How to select only few fields
PostPosted: Wed Apr 16, 2008 8:09 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
>A more elegant and object oriented way, however, of manipulating the >result set is to use dynamic instantiation. ..

Mosa, this is simply awesome..
In case you are aware of spring with hibernate.. this is what I would like to do..

1. In stead of having the class PartialUser as part of my code base, I wish to use Spring to create such temparary classes and use them on demand and through them away.

2. Secondly, I would like to have all these queries in JPA way instead of hibernate HQL way, so that my code could work with any persistance engine.

Could you pl. let me know if both these are possible, if yes, any right sources pl.

Regards,
Nagendra

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


Top
 Profile  
 
 Post subject: Re: How to select only few fields
PostPosted: Wed Apr 16, 2008 8:33 am 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
nagkumar wrote:
>A more elegant and object oriented way, however, of manipulating the >result set is to use dynamic instantiation. ..

Mosa, this is simply awesome..
In case you are aware of spring with hibernate.. this is what I would like to do..

1. In stead of having the class PartialUser as part of my code base, I wish to use Spring to create such temparary classes and use them on demand and through them away.

2. Secondly, I would like to have all these queries in JPA way instead of hibernate HQL way, so that my code could work with any persistance engine.

Could you pl. let me know if both these are possible, if yes, any right sources pl.

Regards,
Nagendra



hi,

to my knowledge :

1. You'll probably going to need the PartialUser class in your code base to use it in other layers and since hibernate provides you with such objects I don't see why have Spring instantiate them. Besides, I don't believe Spring provides such support.

2. Not sure if you could use the same hqp query for both ORM's. I haven't used JPA but I'd be surprised if they used the same query parser! One solution I think is to use a properties file with different keys referring to the different hql's. But, that's just a thought...don't know how this can be actually applied.


mosa


Top
 Profile  
 
 Post subject: Re: How to select only few fields
PostPosted: Wed Apr 16, 2008 9:08 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
>since hibernate provides you with such objects..

Do mean hibernate could create PartialUser (if this physicall exists in my code base) and call the repective settor methods to have the object fully intialised.

I hope Hibernate can't create PartialUser class with out its physical existance.

>2. Not sure if you could use the same hqp query for both ORM's.

o.k, I was hoping there is some JPA standard for queries and which hibernate would support.

Regards,
Nagendra

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


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