EntityNameResolvers in Hibernate

By: Dorris Emailed: 1728 times Printed: 2329 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

org.hibernate.EntityNameResolver is a contract for resolving the entity name of a given entity instance. The interface defines a single method resolveEntityName which is passed the entity instance and is expected to return the appropriate entity name (null is allowed and would indicate that the resolver does not know how to resolve the entity name of the given entity instance). Generally speaking, an org.hibernate.EntityNameResolver is going to be most useful in the case of dynamic models. One example might be using proxied interfaces as your domain model. The hibernate test suite has an example of this exact style of usage under the org.hibernate.test.dynamicentity.tuplizer2. Here is some of the code from that package for illustration.


/**
 * A very trivial JDK Proxy InvocationHandler implementation where we proxy an
 * interface as the domain model and simply store persistent state in an internal
 * Map.  This is an extremely trivial example meant only for illustration.
 */
public final class DataProxyHandler implements InvocationHandler {
    private String entityName;
    private HashMap data = new HashMap();

    public DataProxyHandler(String entityName, Serializable id) {
        this.entityName = entityName;
        data.put( "Id", id );
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String methodName = method.getName();
        if ( methodName.startsWith( "set" ) ) {
            String propertyName = methodName.substring( 3 );
            data.put( propertyName, args[0] );
        }
        else if ( methodName.startsWith( "get" ) ) {
            String propertyName = methodName.substring( 3 );
            return data.get( propertyName );
        }
        else if ( "toString".equals( methodName ) ) {
            return entityName + "#" + data.get( "Id" );
        }
        else if ( "hashCode".equals( methodName ) ) {
            return new Integer( this.hashCode() );
        }
        return null;
    }

    public String getEntityName() {
        return entityName;
    }

    public HashMap getData() {
        return data;
    }
}

public class ProxyHelper {
    public static String extractEntityName(Object object) {
        // Our custom java.lang.reflect.Proxy instances actually bundle
        // their appropriate entity name, so we simply extract it from there
        // if this represents one of our proxies; otherwise, we return null
        if ( Proxy.isProxyClass( object.getClass() ) ) {
            InvocationHandler handler = Proxy.getInvocationHandler( object );
            if ( DataProxyHandler.class.isAssignableFrom( handler.getClass() ) ) {
                DataProxyHandler myHandler = ( DataProxyHandler ) handler;
                return myHandler.getEntityName();
            }
        }
        return null;
    }

    // various other utility methods ....

}

/**
 * The EntityNameResolver implementation.
 *
 * IMPL NOTE : An EntityNameResolver really defines a strategy for how entity names
 * should be resolved.  Since this particular impl can handle resolution for all of our
 * entities we want to take advantage of the fact that SessionFactoryImpl keeps these
 * in a Set so that we only ever have one instance registered.  Why?  Well, when it
 * comes time to resolve an entity name, Hibernate must iterate over all the registered
 * resolvers.  So keeping that number down helps that process be as speedy as possible.
 * Hence the equals and hashCode implementations as is
 */
public class MyEntityNameResolver implements EntityNameResolver {
    public static final MyEntityNameResolver INSTANCE = new MyEntityNameResolver();

    public String resolveEntityName(Object entity) {
        return ProxyHelper.extractEntityName( entity );
    }

    public boolean equals(Object obj) {
        return getClass().equals( obj.getClass() );
    }

    public int hashCode() {
        return getClass().hashCode();
    }
}

public class MyEntityTuplizer extends PojoEntityTuplizer {
    public MyEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
        super( entityMetamodel, mappedEntity );
    }

    public EntityNameResolver[] getEntityNameResolvers() {
        return new EntityNameResolver[] { MyEntityNameResolver.INSTANCE };
    }

    public String determineConcreteSubclassEntityName(Object entityInstance, SessionFactoryImplementor factory) {
        String entityName = ProxyHelper.extractEntityName( entityInstance );
        if ( entityName == null ) {
            entityName = super.determineConcreteSubclassEntityName( entityInstance, factory );
        }
        return entityName;
    }

    ...

In order to register an org.hibernate.EntityNameResolver users must either:

  • Implement a custom Tuplizers (org.hibernate.tuple.Tuplizer), implementing the getEntityNameResolvers method
  • Register it with the org.hibernate.impl.SessionFactoryImpl (which is the implementation class for org.hibernate.SessionFactory) using the registerEntityNameResolver method.

Hibernate Home | All Hibernate Tutorials | Latest Hibernate Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Dorris
sum of all digits in a given number using Shell Script
sh -x and sh -v in Linux
case..esac..in Linux Shell Script
while loop in Linux Shell Script
Nested for loop in Linux Shell Script
for loop in Linux Shell Script
Multilevel if-then-else in Linux Shell Script
Nested if-else-fi in Linux Shell Script
if...else...fi in Linux Shell Script
test command or [ expr ] in Linux Shell Script
if condition in Linux Shell Script
Download Linux. Which linux and where?
Formula in Hibernate
Primary keys assigned by triggers in Hibernate
Assigned identifiers in Hibernate

More Tutorials in Hibernate
Fetching strategies in Hibernate
equals() and hashCode() in Hibernate
Tuplizers (org.hibernate.tuple.Tuplizer) in Hibernate
EntityNameResolvers in Hibernate
Hi/lo algorithm in Hibernate
Identity columns and sequences in Hibernate
Assigned identifiers in Hibernate
Primary keys assigned by triggers in Hibernate
Formula in Hibernate
Step by Step Hibernate - Your First Hibernate Application
A sample Hibernate Web Application using Servlets
Some Basic APIs in Hibernate
Contextual sessions in Hibernate
Programmatic configuration in Hibernate
SessionFactory in Hibernate

More Latest News
Most Viewed Articles (in Hibernate )
Hibernate Vs. JDBC ( A comparison)
Fetching strategies in Hibernate
Step by Step Hibernate - Your First Hibernate Application
Programmatic configuration in Hibernate
Contextual sessions in Hibernate
Tuplizers (org.hibernate.tuple.Tuplizer) in Hibernate
A sample Hibernate Web Application using Servlets
Hibernate Configuration Properties
What is component mapping in hibernate?
Paging Through the Result Set in Hibernate
Tutorial Using the Java Persistence API (JPA) in Hibernate
SessionFactory in Hibernate
Hibernate Transaction Properties
Hibernate JDBC and Connection Properties
EntityNameResolvers in Hibernate
Most Emailed Articles (in Hibernate)
Hibernate Cache Properties
Hibernate Transaction Properties
Identity columns and sequences in Hibernate
Delta Electronics Chairman Bruce Cheng Endows NCKU Build Y.S. Sun Green Building Research Center
Hibernate Vs. JDBC ( A comparison)
Step by Step Hibernate - Your First Hibernate Application
Types of inheritence models in Hibernate
What is component mapping in hibernate?
Paging Through the Result Set in Hibernate
Download Hibernate latest version
Tutorial Using the Java Persistence API (JPA) in Hibernate
A sample Hibernate Web Application using Servlets
Some Basic APIs in Hibernate
Contextual sessions in Hibernate
Programmatic configuration in Hibernate