什么是transaction.commit()在Hibernate? [英] What is transaction.commit() in Hibernate?

查看:662
本文介绍了什么是transaction.commit()在Hibernate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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(),数据库中没有更改。

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?

我是Hibernate的新用户,请帮助我理解。
我使用hibernate 4。

I am new to Hibernate, so please help me understand. I am using hibernate 4.

UPDATE:

我调用 transaction.commit(),然后结果有这一行

IF I call transaction.commit() then the result have this line

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

和数据库也已更新。

这意味着没有调用 transaction.commit(),只有在实例级别上才能完成所有操作,而不改变数据库?

Does this mean that without calling transaction.commit() everything was done only on instance level without really changing the Database?

推荐答案

提交将使数据库提交。持久对象的更改将写入数据库。
Flushing 是在内存中持久化状态持久化
存储的同步过程。
ie。它会在正在运行的
事务中更新或插入到你的表中,但它可能不提交这些更改(这取决于
的刷新模式)。

Commit will make the database commit. The changes to persistent object will be written to database. Flushing is the process of synchronizing the underlying persistent store with persistant state held in memory. ie. it will update or insert into your tables in the running transaction, but it may not commit those changes (this depends on your flush mode).

当你有一个持久化的对象,你改变一个
的值,它变得脏,hibernate需要刷新这些
更改到你的持久层。它可以为
自动执行此操作,或者您可能需要手动执行此操作,这取决于您的冲洗
模式(自动或手动):)

When you have a persisted object and you change a value on it, it becomes dirty and hibernate needs to flush these changes to your persistence layer. It may do this automatically for you or you may need to do this manually, that depends on your flush mode(auto or manual) :)

简而言之: transaction.commit()会刷新会话,但也会结束工作单元。

So in short: transaction.commit() does flush the session, but it also ends the unit of work.

对您的问题的类似参考此处

There is a similar reference to your problem here

这篇关于什么是transaction.commit()在Hibernate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆