-->
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: Updating a single field
PostPosted: Thu Jul 24, 2008 11:36 am 
Beginner
Beginner

Joined: Fri May 16, 2008 3:59 pm
Posts: 30
Please excuse my naivety.

I'm currently querying Position objects with hibernate.
each position has a fund name. for each Positions fund name, I want get a the new price for the fund , then update a single field in the same Position object.

This is what I have right now, but not working:
Will this update the current position object? I just want to update a single field called 'value'.

Code:
public static boolean updatePosition() {
      boolean updated = false;
      Session session = null;
      

      try {
         session = HibernateUtil.getSessionFactory().getCurrentSession();

         session.beginTransaction();

         Query q = session.createQuery("from Position");

         List<Position> positions = q.list();

         for (Iterator<Position> it = positions.iterator(); it.hasNext();) {

            Position position = it.next();

            // For each position's cusip,shares,accountnumber,fundname so we
            // can
            // update that position with new shares*price

            String CUSIP = position.getId().getCusip();
            System.out.println("Looking for cusip: " + CUSIP);
            BigDecimal SHARES = position.getShares();
            
            BigDecimal newPrice = getPrice(CUSIP);
            System.out.println("New Price: " + newPrice);
            if (newPrice != null&&SHARES!=null) {
               
               
               position.setValue(SHARES.multiply(newPrice).setScale(2,
                  BigDecimal.ROUND_CEILING));
               
               
               session.saveOrUpdate(position);
            
               System.out.println("SHARES: "
                     + SHARES
                     + " Price: "
                     + newPrice
                     + " Value: "
                     + SHARES.multiply(newPrice).setScale(2,
                           BigDecimal.ROUND_CEILING));

            }
         }
         
         
      } catch (TransactionException te) {
         te.printStackTrace();
      }
      session.getTransaction().commit();
      updated = true;
      return updated;
   }



Top
 Profile  
 
 Post subject: Updating a single field
PostPosted: Thu Jul 24, 2008 12:02 pm 
Beginner
Beginner

Joined: Fri May 16, 2008 3:59 pm
Posts: 30
Ok, I found my answer to above post.
I have to open another session. I thought I could do it all in one session, but was wrong!

: )


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 4:00 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Thanks for the post-back.

Indeed, you would need another
Hibernate Session in this scenario.

_________________
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  
 
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.