简体   繁体   English

Hibernate的Session.flush()刷新了什么

[英]What's flushed by Hibernate's Session.flush()

What exactly happens when I call Session.flush() on an open hibernate session? 当我在打开的hibernate会话上调用Session.flush()时会发生什么? Are all entities that I changed (persisted, deleted, updated) with save/update/delete written to the database, or are ALL entities in the session written to the database, also if I havn't called update, etc. on them? 我更改(持久化,删除,更新)的所有实体是否已将保存/更新/删除写入数据库,或者是会话中的所有实体都写入数据库,如果我没有在其上调用更新等?

I find lots of Hibernate resources talking about this, but nothing gives me exactly the precise answer I am looking for. 我发现很多Hibernate资源都在讨论这个问题,但没有什么能给我提供我正在寻找的确切答案。

A simple examples: 一个简单的例子:

class A {

    @OneToOne
    public B b;

    public int x;
}

class B {

    @OneToOne(mappedBy="b")
    public B b;

    public int y;
}


// Example
A a = aDao.load(...);
a.x = 20;
b.y = 15;
aDao.update(a);

// Question: Will this update b's value to 15 in the database?
session.flush();

If B will be updated in the example above, how can I prevent it? 如果B将在上面的示例中更新,我该如何防止它?

Thanks for your help! 谢谢你的帮助!

All changes are written to the database. 所有更改都将写入数据库。 Objects that you didn't create/modify/delete aren't saved. 未保存未创建/修改/删除的对象。

I'm not sure what happens for things like obj.setName(obj.getName()) (ie when you touch a property but don't change it). 我不确定obj.setName(obj.getName())类的东西会发生什么(即当你触摸属性但不改变它时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM