在JPA和Hibernate中,persist()和merge()之间有什么区别? [英] What is the difference between persist() and merge() in JPA and Hibernate?

查看:135
本文介绍了在JPA和Hibernate中,persist()和merge()之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Hibernate中,persist()和merge()有什么区别?

What is the difference between persist() and merge() in Hibernate?

persist()可以创建UPDATE& INSERT查询,例如:

persist() can create a UPDATE & INSERT query, eg:

SessionFactory sef = cfg.buildSessionFactory();
Session session = sef.openSession();
A a=new A();
session.persist(a);
a.setName("Mario");
session.flush();

在这种情况下,将这样生成查询:

in this case query will be generated like this:

Hibernate: insert into A (NAME, ID) values (?, ?)
Hibernate: update A set NAME=? where ID=?

因此persist()方法可以生成插入和更新.

so persist() method can generate an Insert and an Update.

现在有merge():

SessionFactory sef = cfg.buildSessionFactory();
Session session = sef.openSession();
Singer singer = new Singer();
singer.setName("Luciano Pavarotti");
session.merge(singer);
session.flush();

这是我在数据库中看到的:

This is what I see in the database:

SINGER_ID   SINGER_NAME
1           Ricky Martin
2           Madonna
3           Elvis Presley
4           Luciano Pavarotti

现在使用merge()

SessionFactory sef = cfg.buildSessionFactory();
Session session = sef.openSession();
Singer singer = new Singer();
singer.setId(2);
singer.setName("Luciano Pavarotti");
session.merge(singer);
session.flush();

这是我在数据库中看到的:

This is what I see in the database:

SINGER_ID   SINGER_NAME
1           Ricky Martin
2           Luciano Pavarotti
3           Elvis Presley

推荐答案

JPA规范包含对这些操作语义的非常精确的描述,比在javadoc中更好:

JPA specification contains a very precise description of semantics of these operations, better than in javadoc:

持久性的语义 操作,应用于实体X 如下:

The semantics of the persist operation, applied to an entity X are as follows:

  • 如果X是一个新实体,则它 被管理.实体X将是 在或之前输入数据库 事务提交或由于 刷新操作.

  • If X is a new entity, it becomes managed. The entity X will be entered into the database at or before transaction commit or as a result of the flush operation.

如果X是一个 先前存在的管理实体 由persist操作忽略. 但是,持久操作是 级联到X引用的实体, 如果从X到这些的关系 其他实体用 cascade=PERSISTcascade=ALL 注释元素值或已指定 具有等效的XML描述符 元素.

If X is a preexisting managed entity, it is ignored by the persist operation. However, the persist operation is cascaded to entities referenced by X, if the relationships from X to these other entities are annotated with the cascade=PERSIST or cascade=ALL annotation element value or specified with the equivalent XML descriptor element.

如果X是已删除的实体, 它成为托管的.

If X is a removed entity, it becomes managed.

如果X是一个 分离的物体 可能会抛出EntityExistsException 当调用持久操作时, 或EntityExistsException或 另一个PersistenceException可能是 在刷新或提交时抛出.

If X is a detached object, the EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException or another PersistenceException may be thrown at flush or commit time.

对于 由a引用的所有实体Y X的关系,如果 与Y的关系已注释 与级联元素值 cascade=PERSISTcascade=ALL, 将持续操作应用于Y.

For all entities Y referenced by a relationship from X, if the relationship to Y has been annotated with the cascade element value cascade=PERSIST or cascade=ALL, the persist operation is applied to Y.


合并操作的语义 应用于实体X的情况如下:

The semantics of the merge operation applied to an entity X are as follows:

  • 如果X是独立实体,则状态为 的X被复制到预先存在的 相同的管理实体实例X' 身份或X的新托管副本X' 已创建.

  • If X is a detached entity, the state of X is copied onto a pre-existing managed entity instance X' of the same identity or a new managed copy X' of X is created.

如果X是新实体 实例,一个新的受管实体 实例X'已创建,状态为 的X被复制到新的托管 实体实例X'.

If X is a new entity instance, a new managed entity instance X' is created and the state of X is copied into the new managed entity instance X'.

如果X是一个 移除的实体实例 IllegalArgumentException将是 由合并操作(或 事务提交将失败).

If X is a removed entity instance, an IllegalArgumentException will be thrown by the merge operation (or the transaction commit will fail).

如果X 是一个受管实体,将被忽略 合并操作,但是, 合并操作级联为 关系引用的实体 如果这些关系具有 被级联注释 元素值cascade=MERGEcascade=ALL注释.

If X is a managed entity, it is ignored by the merge operation, however, the merge operation is cascaded to entities referenced by relationships from X if these relationships have been annotated with the cascade element value cascade=MERGE or cascade=ALL annotation.

对于所有 关系引用的实体Y 来自具有级联元素的X 值cascade=MERGEcascade=ALL,Y 递归合并为Y'.对全部 X引用的Y,X'设置为 参考Y'. (请注意,如果X为 托管,那么X是与相同的对象 X'.)

For all entities Y referenced by relationships from X having the cascade element value cascade=MERGE or cascade=ALL, Y is merged recursively as Y'. For all such Y referenced by X, X' is set to reference Y'. (Note that if X is managed then X is the same object as X'.)

如果X是合并到X'的实体, 参照另一个实体Y, cascade=MERGEcascade=ALL在哪里 未指定,则导航 X'的相同关联产生a 引用管理对象Y' 与Y具有相同的持久身份.

If X is an entity merged to X', with a reference to another entity Y, where cascade=MERGE or cascade=ALL is not specified, then navigation of the same association from X' yields a reference to a managed object Y' with the same persistent identity as Y.

这篇关于在JPA和Hibernate中,persist()和merge()之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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