user1269651
user1269651

Reputation: 184

SaveOrUpdate unresponsive. Why?

@SuppressWarnings("unchecked")
//@Override
public SerialNumber getCounter(String id) {
    Session cs = sessionFactory.getCurrentSession();     
    SerialNumber sn = (SerialNumber) cs.get(SerialNumber.class, id);
    if (sn == null) {
        //TODO Throw an error (maybe). The code below creates a new Serial number counter
        log.debug("NO Serial Number for: " + id + " was found.");
        sn = new SerialNumber();
        sn.setSerialNumberId(id);
        sn.setName("Unspecified");
        sn.setValue(0);
    }
    sn.setValue(sn.getValue()+1);
    cs.saveOrUpdate(sn);
    cs.close();
    return sn;
}

This line is being ignored and saves nothing to the database (MySQL):

cs.saveOrUpdate(sn);

Any ideas as to why? I haven't a clue.

I am by no means Java guru but even the Java guru here cannot figure it out. Any help will be appreciated.

Thanks.

Upvotes: 2

Views: 77

Answers (2)

Reimeus
Reimeus

Reputation: 159864

It is possible that the updates are being deferred for the session. Try:

cs.flush();

If this is your problem, you can set this to AUTO in your properties:

<prop key="org.hibernate.FlushMode">AUTO</prop>

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240966

Try with

Transaction tx = null;
try{
 tx = session.beginTransaction();
   //your code to save
 tx.commit();
 }catch(Exception ex){
     tx.rollBack();
     ex.printstacktrace(); //log to see if there is any other error
 }

Upvotes: 2

Related Questions