001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.orm.hibernate3.support; 018 019import java.io.Serializable; 020import java.util.Map; 021 022import org.hibernate.engine.SessionImplementor; 023import org.hibernate.event.MergeEvent; 024import org.hibernate.event.def.DefaultMergeEventListener; 025import org.hibernate.persister.entity.EntityPersister; 026 027/** 028 * Extension of Hibernate's DefaultMergeEventListener, transferring the ids 029 * of newly saved objects to the corresponding original objects (that are part 030 * of the detached object graph passed into the {@code merge} method). 031 * 032 * <p>Transferring newly assigned ids to the original graph allows for continuing 033 * to use the original object graph, despite merged copies being registered with 034 * the current Hibernate Session. This is particularly useful for web applications 035 * that might want to store an object graph and then render it in a web view, 036 * with links that include the id of certain (potentially newly saved) objects. 037 * 038 * <p>The merge behavior given by this MergeEventListener is nearly identical 039 * to TopLink's merge behavior. See PetClinic for an example, which relies on 040 * ids being available for newly saved objects: the {@code HibernateClinic} 041 * and {@code TopLinkClinic} DAO implementations both use straight merge 042 * calls, with the Hibernate SessionFactory configuration specifying an 043 * {@code IdTransferringMergeEventListener}. 044 * 045 * <p>Typically specified as entry for LocalSessionFactoryBean's "eventListeners" 046 * map, with key "merge". 047 * 048 * @author Juergen Hoeller 049 * @since 1.2 050 * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#setEventListeners(java.util.Map) 051 * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x 052 */ 053@Deprecated 054@SuppressWarnings({"serial", "rawtypes"}) 055public class IdTransferringMergeEventListener extends DefaultMergeEventListener { 056 057 /** 058 * Hibernate 3.1 implementation of ID transferral. 059 */ 060 @Override 061 protected void entityIsTransient(MergeEvent event, Map copyCache) { 062 super.entityIsTransient(event, copyCache); 063 SessionImplementor session = event.getSession(); 064 EntityPersister persister = session.getEntityPersister(event.getEntityName(), event.getEntity()); 065 // Extract id from merged copy (which is currently registered with Session). 066 Serializable id = persister.getIdentifier(event.getResult(), session.getEntityMode()); 067 // Set id on original object (which remains detached). 068 persister.setIdentifier(event.getOriginal(), id, session.getEntityMode()); 069 } 070 071}