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