Hibernate中的merge

本文详细介绍了Hibernate中merge方法的功能及应用场景。当对象ID为空或不存在时,merge将执行插入操作;若ID已存在,则执行更新操作。此外,还介绍了该方法在会话层的行为表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hibernate中的merge方法挺好用的。官方文档的说明如下:

merge
Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".

The semantics of this method are defined by JSR-220.

Parameters:
object - a detached instance with state to be copied
Returns:
an updated persistent instance
Throws:
HibernateException


  有几种情况:
  1.如果idertifier(以下简称为id)为空或不存在,则进行inert动作
  2.如果id存在,则进行update动作
  至于会话层的就如文档上所说,不再多说。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值