transaction.commit()做什么?
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());
这给了我结果:
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
但是由于我没有调用transaction.commit(),所以数据库没有变化。
这是否意味着一切都只在某些实例/对象上完成而没有真正改变数据库?
我是Hibernate的新手,所以请帮助我理解。我正在使用休眠 4。
更新:
如果我调用transaction.commit()那么结果有这一行
Hibernate: update account set balance=? where id=?
并且数据库也更新了。
这是否意味着不调用transaction.commit()一切都只在实例级别完成而没有真正改变数据库?