-->
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: awful code, but still i think this may be hibernate's fault?
PostPosted: Fri Nov 05, 2004 7:02 am 
Newbie

Joined: Fri Oct 08, 2004 9:26 am
Posts: 17
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. ;)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 05, 2004 7:23 am 
Beginner
Beginner

Joined: Tue Jun 22, 2004 7:50 am
Posts: 25
I *think* your main problem is that persisted objects cannot be used across sessions: ie they should only be used with the session that created them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 05, 2004 8:23 am 
Newbie

Joined: Fri Oct 08, 2004 9:26 am
Posts: 17
you're exactly right.

this works perfectly:

public boolean addFoundAuctionIfNotExist(String Market, String AuctionId){
logger.info("Got Auction Number " + AuctionId + " for a " + Market + ".");
List marketList;
boolean retval = true;
try {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();

marketList = session.find("from net.gumb.datatypes.market");



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){
retval = 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){
retval = false;
}
else {
auctionsList.add(thisauction);
thisone.setAuction(auctionsList);

session.save(thisone);

retval = true;
}

tx.commit();
HibernateUtil.closeSession();


}

}
catch (HibernateException e){
logger.info("hibernate exception occured");
marketList = null;
}

return retval;

}

cheers!!


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.