-->
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: Updating Single column of a table
PostPosted: Fri Dec 18, 2009 2:32 am 
Newbie

Joined: Thu Nov 06, 2008 3:42 am
Posts: 15
Hi,
I would like to know how to update single column of a table using Hibernate.
What I do is I get the data from the data base using Restrictions and then I update the column that I want to using
Code:
session.update(object)
. Why I was doing like this is because if I have only that value that I intend to update in the bean the other remaining values vanish.

But I assume there should some other mechanism where in in the bean I set the value that I want to update and Hibernate updates only those values instead of all.

Would you please tell me how to do that?


Top
 Profile  
 
 Post subject: Re: Updating Single column of a table
PostPosted: Fri Dec 18, 2009 5:20 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
You can use dynamic update but there seems to be another bug in your code. If you could show how you get the object you want to update later and what you do with it...


Top
 Profile  
 
 Post subject: Re: Updating Single column of a table
PostPosted: Mon Dec 21, 2009 1:20 am 
Newbie

Joined: Thu Nov 06, 2008 3:42 am
Posts: 15
Hi Below is my code to update a table:
Code:
@SuppressWarnings("unchecked")
   @Override
   public boolean updateData(Object objData) {
      if (objData instanceof SchoolProfile) {
         this.schoolProfile = (SchoolProfile) objData;
         Session session = HibernateUtil.geSessionFactory().openSession();
         Transaction tx = session.beginTransaction();

         List SchoolProfileList = session.createCriteria(SchoolProfile.class).add(
               Restrictions.eq("id", this.schoolProfile.getId())).list();
         if (SchoolProfileList.size() == 0) {
            System.out.println("Id Could not be Found...");
            return false;
         }
         SchoolProfile schprf = (SchoolProfile) SchoolProfileList.get(0);
         schprf.setName(this.schoolProfile.getName());
         schprf.setDescription(this.schoolProfile.getDescription());
         schprf.setRegno(this.schoolProfile.getRegno());
         schprf.setAcstmon(this.schoolProfile.getAcstmon());
         schprf.setAddress(this.schoolProfile.getAddress());
         schprf.setEmail(this.schoolProfile.getEmail());
         schprf.setWebsite(this.schoolProfile.getWebsite());
         session.update(schprf);
         tx.commit();
         session.flush();
         session.close();
         return true;
      }
      return false;
   }

In this ID is the primary key I get it from database and then I update with the values. But some times My update from Controller updates only some column like say Website. Here in this to update only that I have to execute 'set' methods for all the attributes.
My question is - Do I have to retrieve the data from database to update it. Can't I do it directly without retrieving. If yes, Would you please tell how?


Top
 Profile  
 
 Post subject: Re: Updating Single column of a table
PostPosted: Mon Dec 28, 2009 3:05 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
Sorry for my late answer. Christmas...

session.saveOrUpdate(this.schoolProfile);
You don't have to copy any values. Just call save, update or saveOrUpdate (depends on how you created or read the object), let Hibernate do the rest for you.


Top
 Profile  
 
 Post subject: Re: Updating Single column of a table
PostPosted: Mon Dec 28, 2009 11:33 am 
Newbie

Joined: Thu Nov 06, 2008 3:42 am
Posts: 15
Thanks so much.


Top
 Profile  
 
 Post subject: Re: Updating Single column of a table
PostPosted: Fri Jan 08, 2010 3:38 am 
Newbie

Joined: Wed Jan 06, 2010 2:30 am
Posts: 5
Thank ragsh.skgalfor your support.
Hope that you will provide and support more and more efficiently.

_________________
Hibernate guides developer for marketing, It's powerful.


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.