Hibernate:会将工作与多对一的对象进行交互合并? [英] Hibernate: will merge work with many-to-one object transtively?

查看:101
本文介绍了Hibernate:会将工作与多对一的对象进行交互合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我知道并且在合并之前测试会将对象重新连接到会话,防止对象不再处于会话中时发生延迟初始化异常。

。)​​所以我有几个问题。



如果我付款 - >客户(以多对一的单向关系),而且我做了

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

客户对象是否会重新连接到会话中,还是必须为客户再次进行合并呼叫。

b。)如果支付 - >客户(多对一双向关系)会发生什么情况。会发生什么比。

c。)如果我有超过三个层级的关系,那么如何?
例如:hotel - > payment - > customer。

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



谢谢

解决方案

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


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

  • 未初始化的惰性关系是忽略。



相关JPA规范中的exceprt(我猜想原生Hibernate的 Session > interface提供相同的语义):



  • 对于所有由X关系引用的实体Y具有级联元素值
    cascade = MERGE cascade = ALL ,Y会递归合并为Y'。对于X引用的所有这样的Y
    ,X'被设置为引用Y'。 (请注意,如果X被管理,那么X与
    X'是同一个对象。)

  • 如果X是合并到X'的实体,并且引用另一个实体Y ,其中 cascade = MERGE
    cascade = ALL 未指定,则导航同一关联X'产生一个
    引用到一个与Y具有相同持久性标识的被管理对象Y'。


    持久性提供者必须不合并标记为LAZY的字段,这些字段未被提取:合并时必须忽略
    这样的字段。


请参阅还有:


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

解决方案

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:

这篇关于Hibernate:会将工作与多对一的对象进行交互合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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