-->
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: Update a column: Optimised Way
PostPosted: Tue Dec 22, 2009 1:03 am 
Newbie

Joined: Thu Nov 06, 2008 3:42 am
Posts: 15
Hi Below is my code to update a table:
Code:
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 IDis 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: Update a column: Optimised Way
PostPosted: Tue Dec 22, 2009 4:16 am 
Beginner
Beginner

Joined: Tue Nov 03, 2009 9:38 am
Posts: 24
I'm not sure I fully understand your problem, but you can use the executeUpdate() method on a Query object and use JPQL like
"UPDATE Entity entity SET entity.field = :someValue".


Top
 Profile  
 
 Post subject: Re: Update a column: Optimised Way
PostPosted: Mon Dec 28, 2009 11:34 am 
Newbie

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


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.