-->
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.  [ 6 posts ] 
Author Message
 Post subject: Select only certain columns
PostPosted: Wed Jun 25, 2008 5:21 am 
Newbie

Joined: Wed Jun 25, 2008 5:15 am
Posts: 3
Hibernate version: last

Name and version of the database you are using: mysql v. 5

Hello, it's the first time i use hibernate.
Is it possible to load just some columns? For example load an User object querying just the ID and the Name (not the password or the email address and so on).
I saw something about this but returned an ugly list of array of objects. Is it possible to have a User-object?
Will it be updatable?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 10:25 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
So, you want to get just the ID and name, but you don't want to get the whole user, and at the same time, you don't want the data returned as an int/String combination in an array?

Just work with the objects. That's what Hibernate is all about. Do the query, get the object, and then calll the getId() and getName() methods on the object. That's how Hibernate works. Here's an example from my Hibernate Tutorials website:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=08masteringhqlandnamedqueries

Code:
Session session = HibernateUtil.beginTransaction();
String hql="from User where loginName = 'mj' ";
Query query = session.createQuery(hql);
Object o  = query.uniqueResult();
User u = (User)o;
System.out.println(u.getLoginName());
System.out.println("\n\n");
HibernateUtil.commitTransaction();


It's all about object-oriented programming, and taking an object oriented approach to a database. If that's not appropriate for your scenario, then Hibernate isn't the right tool. But I have a feeling it is the right tool.

Change your perspective, and think now of your data in terms of objects, not columns and tables. You'll find it to be a very freeing experience.

http://jpa.thebookonhibernate.com/Javacode/learn.jsp?tutorial=08masteringhqlandnamedqueries

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 1:12 pm 
Newbie

Joined: Wed Jun 25, 2008 5:15 am
Posts: 3
Thank you for the answer Cameron. The problem is not thinking of data as object instead of columns, the problem is performance.
The code you wrote what sql would execute?
I see just two possibilities:

SELECT * FROM UserTable WHERE ...
or when i call getLoginName() (i think this is the right one)
SELECT username FROM UserTable WHERE ...

Both of them are resource expensive! The former gets useless data, the latter makes a lot of mini-query.

I just need something that maps a
SELECT id, username FROM UserTable WHERE ...
in an object, just a single query that obtains all the informations i need and allow me to edit them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 3:19 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, I think you might be underestimating how little there is in difference between fetching those two queries. Assuming the data is all in the same table, the difference in performance would be negligible.

Regardless, you can always just execute SQL, but yes, to your original point, you simply get back an array with corresonding object types that map to the table.

Quote:
Now one thing to note about native SQL queries is that what gets returned in each element of the List is simply an Object array, containing the datatype to which the queried columns map, as defined by the JPA annotations of the class. Furthermore, with a SELECT * query, we would need to know the order of the columns in the database so we can cast the incoming data properly.

The following is an example of a native SQL query that goes against a User database table with id (Integer), email, name and password fields:



http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=08masteringhqlandnamedqueries

Code:
public static void main(String args[]) {
  String sql = "SELECT * FROM USER";
  Session session = HibernateUtil.beginTransaction();
  SQLQuery query = session.createSQLQuery(sql);
  List users = query.list();
  for (int i = 0; i < users.size(); i++) {
    Object[] o = (Object[]) users.get(i);
    System.out.print(((Integer) o[0])); //id
    System.out.print(((String) o[1]));  //email
    System.out.print(((String) o[2]));  //name
    System.out.println(((String) o[3]));//pass
  }
}



http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=08masteringhqlandnamedqueries

Think about the long term maintainability of your program though. If you query on columns, and those columns ever change, your SQL all over you program will change. If you just use the mapped objects, all you have to change is an annotation in one place or another.

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 25, 2008 3:40 pm 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
I have not heard about selecting particular columns in a table while using mapped objects:(

Since you want something custom, go-ahead with your own HQL...Or the best other way would be separating your usertable into USER and LOGIN table..by which i hope you will achieve your expectation.

Hope this helps:-)

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 5:03 am 
Newbie

Joined: Fri Jan 28, 2005 3:45 pm
Posts: 1
Does this solve your problems?

http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#queryhql-select

look for "select new Family(mother, mate, offspr)"


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