01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.springframework.orm.hibernate3.support;
18:
19: import org.hibernate.EmptyInterceptor;
20:
21: import org.springframework.aop.scope.ScopedObject;
22: import org.springframework.aop.support.AopUtils;
23:
24: /**
25: * Hibernate3 interceptor used for getting the proper entity name for scoped
26: * beans. As scoped bean classes are proxies generated at runtime, they are
27: * unrecognized by the persisting framework. Using this interceptor, the
28: * original scoped bean class is retrieved end exposed to Hibernate for
29: * persisting.
30: *
31: * <p>Usage example:
32: *
33: * <pre>
34: * <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
35: * ...
36: * <property name="entityInterceptor">
37: * <bean class="org.springframework.orm.hibernate3.support.ScopedBeanInterceptor"/>
38: * </property>
39: * </bean></pre>
40: *
41: * @author Costin Leau
42: * @author Juergen Hoeller
43: * @since 2.0
44: */
45: public class ScopedBeanInterceptor extends EmptyInterceptor {
46:
47: public String getEntityName(Object entity) {
48: if (entity instanceof ScopedObject) {
49: // Determine underlying object's type.
50: Object targetObject = ((ScopedObject) entity)
51: .getTargetObject();
52: return AopUtils.getTargetClass(targetObject).getName();
53: }
54:
55: // Any other object: delegate to the default implementation.
56: return super.getEntityName(entity);
57: }
58:
59: }
|