KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > orm > hibernate3 > support > ScopedBeanInterceptor


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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  * &lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate3.LocalSessionFactoryBean&quot;&gt;
35  * ...
36  * &lt;property name=&quot;entityInterceptor&quot;&gt;
37  * &lt;bean class=&quot;org.springframework.orm.hibernate3.support.ScopedBeanInterceptor&quot;/&gt;
38  * &lt;/property&gt;
39  * &lt;/bean&gt;</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 JavaDoc getEntityName(Object JavaDoc entity) {
48         if (entity instanceof ScopedObject) {
49             // Determine underlying object's type.
50
Object JavaDoc targetObject = ((ScopedObject) entity).getTargetObject();
51             return AopUtils.getTargetClass(targetObject).getName();
52         }
53
54         // Any other object: delegate to the default implementation.
55
return super.getEntityName(entity);
56     }
57
58 }
59
Popular Tags