-->
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: how to update/load an object with only specific columns
PostPosted: Tue Jun 10, 2008 10:57 am 
Newbie

Joined: Tue Jun 10, 2008 10:32 am
Posts: 4
i have a table with large columns (Text , blob). after persisting my object, i need to be able to modify only some specific columns to avoid loading or inserting large data columns that are not used for some specific cases like updating status. what is the best way to handle these kind of issues using hibernate session.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 11:03 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
It's not always a path I recommend, but if you really need specific functionality to avoid a major performance hit, you can always issue native SQL through the Hibernate Session.

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
  }
}


This code snippet was taken from the following Hiberante3 tutorial:

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

Of course, you can also do custom HQL as well. It's just a matter of weighing the pros and cons.

_________________
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: Thu Jun 12, 2008 12:35 am 
Newbie

Joined: Wed Jun 11, 2008 7:51 am
Posts: 8
- I use Hibernate's basic operations: persist(), saveOrUpdate(), merge(), delete(), replicate().

- Plus HQL with custom objets in the select as in these examples to retreive the columns I need....select new(field1, field2, field3) from...

if you don't alter the PKs or FKs it's supposed to be easy.


Some more ex to retrevie the columns you need.

Queries may return multiple objects and/or properties as an array of type Object[],

select mother, offspr, mate.name
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr

or as a List,

select new list(mother, offspr, mate.name)
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr

or as an actual typesafe Java object,

select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr


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.