Java REFRESH 和 MERGE 在数据库方面是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/836569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:02:16  来源:igfitidea点击:

What do REFRESH and MERGE mean in terms of databases?

javajpapersistence

提问by André Chalella

I'm curious and need to find this answer quick. Google won't help much.

我很好奇,需要快速找到这个答案。谷歌不会有太大帮助。

The Java Persistence API has these properties that tell the framework to cascade operations on associated entities:

Java Persistence API 具有这些属性,它们告诉框架在关联实体上级联操作:

CascadeType.PERSIST
CascadeType.DELETE
CascadeType.MERGE
CascadeType.REFRESH

I know what the first two mean: when I persist object A which has B, persist B as well, and when I delete A, delete B as well.

我知道前两个是什么意思:当我持久化有 B 的对象 A 时,也持久化 B,当我删除 A 时,也删除 B。

But I can't make any sense of what the other two accomplish. Help?

但我无法理解其他两个完成了什么。帮助?

采纳答案by chaos

REFRESH means "pull any state changes from the database into my representation". Cascading this is simple; it means that all associated entities are refreshed.

REFRESH 表示“将任何状态更改从数据库中提取到我的表示中”。级联这很简单;这意味着所有关联的实体都被刷新。

MERGE means something complex that approximates "save" but is more like "push this detached entity back into managed status and save its state changes"; the cascading means that all associated entities get pushed back the same way, and the managed-entity handle you get back from .merge()has all managed entities associated with it.

MERGE 意味着类似于“保存”的复杂事物,但更像是“将这个分离的实体推回到托管状态并保存其状态更改”;级联意味着所有关联的实体都以相同的方式被推回,并且您从中获得的托管实体句柄.merge()具有与其关联的所有托管实体。

Link to one instance of the relevant docs

链接到相关文档的一个实例

回答by cgp

JPA Annotation Meaning for Many to Many relationships:

多对多关系的 JPA 注释含义

  • ALL - all possible cascading operations performed on the source entity are cascaded to the target of the association.
  • MERGE - if the source entity is merged, the merge is cascaded to the target of the association.
  • PERSIST - if the source entity is persisted, the persist is cascaded to the target of the association.
  • REFRESH - if the source entity is refreshed, the refresh is cascaded to the target of the association.
  • REMOVE - if the source entity is removed, the target of the association is also removed.
  • ALL - 在源实体上执行的所有可能的级联操作都级联到关联的目标。
  • MERGE - 如果源实体被合并,则合并级联到关联的目标。
  • PERSIST - 如果源实体被持久化,则持久化将级联到关联的目标。
  • REFRESH - 如果源实体被刷新,则刷新级联到关联的目标。
  • REMOVE - 如果源实体被移除,关联的目标也会被移除。

I myself see them this way (more readable):

我自己是这样看待它们的(更具可读性):

  • MERGE - for existing objects, to mergethe existing data in the table with the data in my object. (sync to database)
  • PERSIST - is create new records from object in the database.
  • REFRESH - is to refresh the data in the object. Perhaps there was a change on the database which needs to be synced. (sync from database)
  • REMOVE - is, well, delete.
  • MERGE - 对于现有对象,表中的现有数据与我的对象中的数据合并。(同步到数据库)
  • PERSIST - 从数据库中的对象创建新记录。
  • REFRESH - 是刷新对象中的数据。也许需要同步的数据库发生了变化。(从数据库同步)
  • REMOVE - 是,删除。