1
package org.hibernate.test.dynamicentity.interceptor;
3
import org.hibernate.EmptyInterceptor;
4
import org.hibernate.EntityMode;
5
import org.hibernate.test.dynamicentity.Company;
6
import org.hibernate.test.dynamicentity.Customer;
7
import org.hibernate.test.dynamicentity.ProxyHelper;
8
import org.hibernate.test.dynamicentity.DataProxyHandler;
10
import java.io.Serializable;
11
import java.lang.reflect.InvocationHandler;
12
import java.lang.reflect.Proxy;
15
* Our custom {@link org.hibernate.Interceptor} impl which performs the
16
* interpretation of entity-name -> proxy instance and vice-versa.
18
* @author <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
20
public class ProxyInterceptor extends EmptyInterceptor {
23
* The callback from Hibernate to determine the entity name given
24
* a presumed entity instance.
26
* @param object The presumed entity instance.
27
* @return The entity name (pointing to the proper entity mapping).
29
public String getEntityName(Object object) {
30
String entityName = ProxyHelper.extractEntityName( object );
31
if ( entityName == null ) {
32
entityName = super.getEntityName( object );
38
* The callback from Hibernate in order to build an instance of the
39
* entity represented by the given entity name. Here, we build a
40
* {@link Proxy} representing the entity.
42
* @param entityName The entity name for which to create an instance. In our setup,
43
* this is the interface name.
44
* @param entityMode The entity mode in which to create an instance. Here, we are only
45
* interestes in custom behavior for the POJO entity mode.
46
* @param id The identifier value for the given entity.
47
* @return The instantiated instance.
49
public Object instantiate(String entityName, EntityMode entityMode, Serializable id) {
50
if ( entityMode == EntityMode.POJO ) {
51
if ( Customer.class.getName().equals( entityName ) ) {
52
return ProxyHelper.newCustomerProxy( id );
54
else if ( Company.class.getName().equals( entityName ) ) {
55
return ProxyHelper.newCompanyProxy( id );
58
return super.instantiate( entityName, entityMode, id );