关于merge与IdTransferringMergeEventListener的用法

本文详细介绍了Hibernate框架中merge方法的功能及使用方式。当使用此方法时,它会将提供的对象的状态复制到具有相同标识符的持久化对象上,并遵循JSR-220规范。类似于saveOrUpdate方法,但不会将提供的对象与当前Hibernate会话相关联。如果遇到新实体,则会将状态复制过去。需要注意的是,merge方法不会更新传递的对象图中的标识符。

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

newObj = getHibernateTemplate().merge(obj);

obj是一个游离对象,该方法将把obj中的属性更新到与obj主键一致的持久对象中,并返回该持久对象newObj。如果没有对应的持久对象,将会新建一个持久对象并将属性拷贝进去,并返回新对象。

参考:

Copy the state of the given object onto the persistent object with the same identifier. Follows JSR-220 semantics.

Similar to saveOrUpdate , but never associates the given object with the current Hibernate Session. In case of a new entity, the state will be copied over as well.

Note that merge will not update the identifiers in the passed-in object graph (in contrast to TopLink)! Consider registering Spring's IdTransferringMergeEventListener if you would like to have newly assigned ids transferred to the original object graph too.

 

如果上述方法新建了一个持久对象,持久对象的id并不会拷贝到obj中。如果想要拷贝到obj中,要注册IdTransferringMergeEventListener 监听器。

例如:

xml 代码
		<property name="eventListeners">
			<map>
				<entry key="merge">
					<bean
						class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
				</entry>
			</map>
		</property>
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值