this is a method i wrote, it's supposed to check if a particular market (idenified by parameter Market) contains an auction with Id AuctionId and then if not then add it to the list collection which is an attbute of the hibernate object.
public boolean addFoundAuctionIfNotExist(String Market, String AuctionId){
logger.info("Got Auction Number " + AuctionId + " for a " + Market + ".");
List marketList;
try {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
marketList = session.find("from net.gumb.datatypes.market");
tx.commit();
HibernateUtil.closeSession();
}
catch (HibernateException e){
logger.info("hibernate exception occured");
marketList = null;
}
ListIterator mktIter = marketList.listIterator();
market thisone = null;
while (mktIter.hasNext()){
market k = (market) mktIter.next();
if (k.getName().equals(Market)){ thisone = k; logger.info("yay " + k.getName()); continue;}
}
if (thisone == null){
return false;
}
else {
auction thisauction = new auction();
thisauction.setAuctionNo(AuctionId);
List auctionsList = thisone.getAuction();
ListIterator auctIter = auctionsList.listIterator();
while (auctIter.hasNext()){
auction w = (auction) auctIter.next();
if (w.getAuctionNo().equals(AuctionId)) { thisauction = null; continue; }
}
if (thisauction == null){
return false;
}
else {
auctionsList.add(thisauction);
thisone.setAuction(auctionsList);
try {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.save(thisone);
tx.commit();
HibernateUtil.closeSession();
}
catch (HibernateException e){
logger.info("hibernate exception occured");
marketList = null;
}
return true;
}
}
}
the logic of it works fine, but the problem lies at the end, as my coding elegence is low to non existence i opened a second hibernate session if the auction needs to be added (this should only happen say 10% of the time).
anyway what hibernate does is create a new market object each time it finds a new auction, so i end up with as many market objects as auctions, and a new complete set of auctions added each time i run the script which calls this method.
please can someone tell me the main places where i am retarded and what is most significantly sqew-whiff about this code.
thanks a million. ;)
|