就数据库而言,REFRESH 和 MERGE 意味着什么?

我很好奇,需要尽快找到这个答案。谷歌帮不上什么忙。

Java持久化API 的这些属性告诉框架对关联实体进行级联操作:

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

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

但我搞不懂另外两个人做了什么,帮忙?

77615 次浏览

JPA Annotation Meaning for Many to Many relationships:

  • 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.

I myself see them this way (more readable):

  • MERGE - for existing objects, to merge the 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.

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

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.

Link to one instance of the relevant docs