Hibernate:会以传递方式合并多对一对象的工作吗?

发布于 2024-10-16 03:23:41 字数 453 浏览 1 评论 0原文

您好,我知道在合并之前进行测试会将对象重新附加回会话,以防止当对象不再处于会话中时发生延迟初始化异常。

a.) 所以我有几个问题。

如果我付款-->客户(处于多对一的单向关系)和我进行

Payment payment = Payment.class.cast(session.merge(oldPayment));

客户对象是否也会重新附加到会话中,或者我是否必须为客户进行另一个合并调用。

b.) 如果付款--> 会发生什么?客户(多对一的双向关系)。比会发生什么。

c.) 如果我有超过三个层次结构的关系怎么样?
示例:酒店 -->付款-->顾客。

如果我执行 Hotel hotel = Hotel.class.cast(session.merge(unmergeHotel)),付款和客户对象也会合并到会话中吗?

谢谢

Hi I know that and tested before merge will reattach the object back to session preventing lazy initialization exception when object is no longer in session.

a.) So I have a few question.

If i payment --> customer (in a many-to-one unidirectional relationship) and I do

Payment payment = Payment.class.cast(session.merge(oldPayment));

Will customer object also be reattach into session, or do I have to make another merge call for the customer.

b.) What happen if the payment--> customer (many-to-one bidirectional relationship). What would happen than.

c.) How about if i have relationship of more than three hierarchy.
example: hotel --> payment --> customer.

If I do Hotel hotel = Hotel.class.cast(session.merge(unmergeHotel)), will the payment and customer object also be merge into session?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她比我温柔 2024-10-23 03:23:41

它是由关系的级联选项定义的。

  • 如果关系配置为级联合并操作,则从正在合并的实体引用的实体也将被合并,以便合并之前对这些实体所做的更改将传播到数据库。
  • 否则,这些实体将从数据库中重新加载,因此在合并之前对这些实体所做的任何更改都将被丢弃。
  • 未初始化的惰性关系将被忽略。

JPA 规范的相关摘录(我猜原生 Hibernate 的 Session 接口提供了相同的语义):

  • 对于由具有级联元素值的 X 的关系引用的所有实体 Y
    cascade=MERGEcascade=ALL,Y 递归合并为 Y'。对于所有此类 Y 引用
    通过X,X'被设置为参考Y'。 (请注意,如果 X 是托管的,则 X 与
    X'。)
  • 如果 X 是合并到 X' 的实体,并引用另一个实体 Y,其中 cascade=MERGE
    cascade=ALL 未指定,则从 X' 导航相同的关联会产生
    对与 Y 具有相同持久标识的托管对象 Y' 的引用。

持久性提供程序不得合并尚未获取的标记为 LAZY 的字段:它必须忽略
合并时出现此类字段。

另请参阅:

It's defined by cascading options of your relationships.

  • If relationship is configured to cascade merge operations, then entities referenced from the entity being merged will be merged too, so that changes made to these entities before merge will be propagated to the database.
  • Otherwise, these entities will be reloaded from the database, therefore any changes made to these entities before merge will be discarded.
  • Uninitialized lazy relationships are ignored.

Related exceprt from JPA Specification (I guess native Hibernate's Session interface offers the same semantics):

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

The persistence provider must not merge fields marked LAZY that have not been fetched: it must ignore
such fields when merging.

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文