Java – transaction. In hibernate What is commit()?

transaction. What does 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 the 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 because I didn't call transaction Commit (), so the database has not changed

Does this mean that everything is done only on some instances / objects without really changing the database?

I'm new to hibernate, so please help me understand I am using hibernate 4

to update:

If I call transaction Commit(), then the result has this line

Hibernate: update account set balance=? where id=?

The database has also been updated

Does this mean that transaction. Is not called Commit() does everything at the instance level without actually changing the database?

Solution

Commit will cause the database to commit Changes to persistent objects are written to the database

When you have a persistent object, you change one of its values, it becomes dirty, sleep needs to refresh these changes to your persistent layer It can be performed automatically or you may need to perform it manually, depending on your flush mode (automatic or manual):

So in short: transaction Commit () refreshes the session, but it also ends the unit of work

There is a similar reference to your question here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>