What does transaction.commit() do?
Account account = new Account();
account.setId(100);
account = (Account) session.get(Account.class, account.getId());
System.out.println("Before Transaction: Balance = " + account.getBalance());
double preBal = account.getBalance();
account.setBalance(50000000);
Transaction transaction = session.beginTransaction();
session.update(account);
account = (Account) session.get(Account.class, account.getId());
System.out.println("After Transaction: Balance = " + account.getBalance());
// transaction.commit();
account = (Account) session.get(Account.class, account.getId());
System.out.println("Pev-Bal=" + preBal + " Curr-Bal=" + account.getBalance());
This gives me result:
Hibernate: select account0_.id as id0_1_, account0_.balance as ..........
Before Transaction: Balance = 300.0
After Transaction: Balance = 5.0E7
Pev-Bal=300.0 Curr-Bal=5.0E7
But since I did not call transaction.commit() there was no change in Database.
Does this means everything was done only on some instance/objects without really changing the Database?
I am new to Hibernate, so please help me understand. I am using hibernate 4.
UPDATE:
IF I call transaction.commit() then the result have this line
Hibernate: update account set balance=? where id=?
And Database also updated.
Does this mean that without calling transaction.commit() everything was done only on instance level without really changing the Database?