开发者

What's flushed by Hibernate's Session.flush()

开发者 https://www.devze.com 2023-03-13 21:32 出处:网络
What exactly happens when I call Session.flush() on an open hibernate session? Are all entities that I changed (persisted, deleted, updated) with save/update/delete written to the database, or are ALL

What exactly happens when I call Session.flush() on an open hibernate session? 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.

A si开发者_开发问答mple 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?

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()) (i.e. when you touch a property but don't change it).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号