-->
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: Update just some columns
PostPosted: Tue Jun 05, 2007 3:52 am 
Newbie

Joined: Fri May 25, 2007 4:03 am
Posts: 15
Hi!
I load just, for example 3 columns (a,b,c) from my table(A) - which also contains more columns like x,y,z.

now i wanna load just these 3 columns:
Code:
public static List<A> readBatchData() {
Session session = HibernateUtil.getCurrentSession();
Transaction tx = null;
ArrayList<A> values = null;
try {
       tx = session.beginTransaction();
       /* read special columns from db */
      values = (ArrayList<A>)session.createQuery   
        (select new A(a,b,c) from A).list();
      /* commit and close session */
      tx.commit();      
} catch (Exception e) {
    if (tx != null) {
      tx.rollback();
    }
} finally {      
   session = null;
   tx = null;
}
  return values;
}


Then i update the data and then i wanna update these colums at the database table:
Code:
   public static void updateBatchData(final List<A> updateList) {
      Session session = HibernateUtil.getCurrentSession();
      Transaction tx = null;
      try {
         tx = session.beginTransaction();
         
         for (A model : updateList) {
            /* update */
            session.saveOrUpdate(model);
         }         
         tx.commit();   
      } catch (Exception e) {
         if (tx != null) {
            tx.rollback();
         }
      } finally {
         session = null;
         tx = null;
      }
   }


But if i do it like that, then the other columns (x,y,z) will be set NULL - any solutions?

Second question:
when
Code:
session.saveOrUpdate(model);
get called thats already a database interaction (columns get set at db?) or the model got saved at the hibernate session until
Code:
tx.commit()
get called?

best regards.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 5:40 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi y0dA,
use the interface type List instead of ArrayList (hibernate has its own List implementation). And you don't need to cast when retrieving the result set.
Just :
Code:
List values = null;
....
values = session.createQuery   
        (select new A(a,b,c) from A).list();
...

_________________
chucky

Don't forget to rate if this helps
-----------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 5:42 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
For the second question:
database synchronization happens only when you commit or call explicitly flush on the session

_________________
chucky

Don't forget to rate if this helps
-----------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 05, 2007 5:52 am 
Newbie

Joined: Fri May 25, 2007 4:03 am
Posts: 15
Hi!

chucky wrote:
Hi y0dA,
use the interface type List instead of ArrayList (hibernate has its own List implementation). And you don't need to cast when retrieving the result set.
Just :
Code:
List values = null;
....
values = session.createQuery   
        (select new A(a,b,c) from A).list();
...


Loading isnt the problem (but thanks for this), the problem is the update which override all other columns with null.

Quote:
For the second question:
database synchronization happens only when you commit or call explicitly flush on the session

So the model still stay at the session until i do a commit or flush?

best regards


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 9:00 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
As far as I understand your updateList contains newly created object instances that have only those values set that you want to be changed in the database.
Hibernate's default behaviour is to save all of the object's fields, since you might want to set the null-values in the database.
If you want only non-null-values to be updated in the database you'll have to set dynamic-update="true" in your class-mapping.


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.