Hi, I've been having a really strange problem, for some reason when I run my web application
in Tomcat and this method is executed it claims to have worked says row = 1 and consequently
prints out the message "Updated Row: 1". However, when I look at my mySQL database the attribute
has not changed its value and it is still NULL. I've manually run the method (outside of Tomcat) with a main method and it DOES update the attribute in my MySQL database. Any ideas what's going wrong? The other things is, this is not my only interaction my web-app has with the database and the other interactions work fine.
Code:
public void attributeUpdater(User user, Map<String,String> attribMap){
Set<String> keySet = attribMap.keySet();
Iterator<String> it = keySet.iterator();
String colName = "";
String colValue = "";
String userName = user.getLocalName();
DebugOut.write("The size of attribMap is\t"+attribMap.size());
while(it.hasNext()){
colName = it.next();
colValue = attribMap.get(colName);
Session session = factory.currentSession();
try {
session.beginTransaction();
String hql = "update User set "+colName+" = '"+colValue+"' where userName = '"+userName+"'";
DebugOut.write(hql);
Query query = session.createQuery(hql);
int row = query.executeUpdate();
session.getTransaction().commit();
if (row == 0){
DebugOut.write("DIDN'T UPDATE");
System.out.println("Hasn't updated any row!");
}
else{
DebugOut.write("SHOULD HAVE UPDATED");
DebugOut.write("Updated Row:" + row);
System.out.println("Updated Row: " + row);
}
} catch (RuntimeException e) {
DebugOut.write("RUNTIME EXCEPTION THROWN\t"+e.getMessage());
session.getTransaction().rollback();
throw new HibernateException("Error loading entity. " + e.getMessage(), e);
} catch (Exception e) {
DebugOut.write("EXCEPTION THROWN");
session.getTransaction().rollback();
throw new HibernateException("Error loading entity. " + e.getMessage(), e);
} finally {
factory.closeSession();
}
}
}
Thanks