Amit Das
Amit Das

Reputation: 1107

session.update() or session.save() of data is not reflecting in the database?

I am working on web application build using spring mvc 3.2 and hibernate 3.6.3 I am extedning HibernateDaoSupport class in my DAO classes. Now i want to update a entity using session.update() in which i am getting session from hibernateDaoSupport method getSession(). I am using transaction also in this. But after committing the transaction the update is not reflecting in the database. But if i am using session.flush() before commit, then its getting reflected. As i know that transaction.commit calls flush before commit, then why it is not updating record. I am not using any transaction manager in my application context then how transaction is managed by hibernatedaosupport. when a new session will open in my application because getSession method check for session from current transaction, if it found that then it returns that else new. So that means a new hibernate session will open in each http request or not ?

My Dao Code is -

public Map<String, Object> addImmunizationCard(Box box,
            Map<String, Object> dataMap) {
        Session session = (Session) getSession();
        Map<String, Object> map = new HashMap<String, Object>();
        Transaction tx = null;
try {
            tx = session.beginTransaction();

MasBed bed = (MasBed)session.get(MasBed.class, new Integer(1101));

            System.out.println(bed.getBedNo());
            bed.setBedNo("Amit Das");
            session.update(bed);
            tx.commit();

        } catch (Exception e) {
            e.printStackTrace();
            tx.rollback();
        }
        return map;
}

Upvotes: 1

Views: 1376

Answers (1)

OTM
OTM

Reputation: 656

The default flush mode is AUTO which means its sometimes flushed before query execution. When the flush mode is set to COMMIT then flush is done before transaction commit.

Please see https://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/FlushMode.html

Upvotes: 0

Related Questions