|
Hibernate example source code file (DynamicInstantiator.java)
The Hibernate DynamicInstantiator.java source code//$Id$ package org.hibernate.test.annotations.tuplizer; import java.io.Serializable; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import org.hibernate.HibernateException; import org.hibernate.internal.util.ReflectHelper; import org.hibernate.tuple.Instantiator; /** * @author Emmanuel Bernard */ public class DynamicInstantiator implements Instantiator { private final String entityName; public DynamicInstantiator(String entityName) { this.entityName = entityName; } public Object instantiate(Serializable id) { if ( Cuisine.class.getName().equals( entityName ) ) { return ProxyHelper.newCuisineProxy( id ); } if ( Country.class.getName().equals( entityName ) ) { return ProxyHelper.newCountryProxy( id ); } else { throw new IllegalArgumentException( "unknown entity for instantiation [" + entityName + "]" ); } } public Object instantiate() { return instantiate( null ); } public boolean isInstance(Object object) { String resolvedEntityName = null; if ( Proxy.isProxyClass( object.getClass() ) ) { InvocationHandler handler = Proxy.getInvocationHandler( object ); if ( DataProxyHandler.class.isAssignableFrom( handler.getClass() ) ) { DataProxyHandler myHandler = ( DataProxyHandler ) handler; resolvedEntityName = myHandler.getEntityName(); } } try { return ReflectHelper.classForName( entityName ).isInstance( object ); } catch( Throwable t ) { throw new HibernateException( "could not get handle to entity-name as interface : " + t ); } // return entityName.equals( resolvedEntityName ); } } Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate DynamicInstantiator.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.