Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmanuelbernard/672755 to your computer and use it in GitHub Desktop.
Save emmanuelbernard/672755 to your computer and use it in GitHub Desktop.
Version selecting the right delegate
package org.jboss.seam.persistence;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.CacheMode;
import org.hibernate.Criteria;
import org.hibernate.EntityMode;
import org.hibernate.Filter;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.LobHelper;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.ReplicationMode;
import org.hibernate.SQLQuery;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.TypeHelper;
import org.hibernate.UnknownProfileException;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.engine.ActionQueue;
import org.hibernate.engine.EntityEntry;
import org.hibernate.engine.EntityKey;
import org.hibernate.engine.LoadQueryInfluencers;
import org.hibernate.engine.NonFlushedChanges;
import org.hibernate.engine.PersistenceContext;
import org.hibernate.engine.QueryParameters;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.engine.query.sql.NativeSQLQuerySpecification;
import org.hibernate.event.EventListeners;
import org.hibernate.event.EventSource;
import org.hibernate.impl.CriteriaImpl;
import org.hibernate.jdbc.Batcher;
import org.hibernate.jdbc.JDBCContext;
import org.hibernate.jdbc.Work;
import org.hibernate.loader.custom.CustomQuery;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.search.FullTextSession;
import org.hibernate.stat.SessionStatistics;
import org.hibernate.type.Type;
import com.sun.org.apache.xml.internal.serializer.ElemDesc;
/**
* InvocationHandler that proxies the Session, and implements EL interpolation
* in HQL. Needs to implement SessionImplementor because DetachedCriteria casts
* the Session to SessionImplementor.
*
* @author Gavin King
* @author Emmanuel Bernard
* @author Mike Youngstrom
* @author Marek Novotny
*
*/
public class HibernateSessionInvocationHandler implements InvocationHandler, Serializable, EventSource
{
private FullTextSession ftDelegate;
private Session delegate;
Object getDelegate(Method method)
{
if (isPureEventSourceMethod(method))
{
return delegate;
}
else
{
return ftDelegate;
}
}
boolean isPureEventSourceMethod(Method method)
{
//detect if Method is hosted on EventSource and *not* any of its interfaces
//if true returns true otherwise return false
}
public HibernateSessionInvocationHandler(Session paramDelegate, FullTextSession searchDelegate)
{
this.ftDelegate = searchDelegate;
this.delegate = paramDelegate;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
try
{
if ("createQuery".equals(method.getName()) && method.getParameterTypes().length > 0 && method.getParameterTypes()[0].equals(String.class))
{
return handleCreateQueryWithString(method, args);
}
if ("reconnect".equals(method.getName()) && method.getParameterTypes().length == 0)
{
return handleReconnectNoArg(method);
}
return method.invoke(getDelegate(method), args);
}
catch (InvocationTargetException e)
{
throw e.getTargetException();
}
}
protected Object handleCreateQueryWithString(Method method, Object[] args) throws Throwable
{
if (args[0] == null)
{
return method.invoke(getDelegate(method), args);
}
String ejbql = (String) args[0];
if (ejbql.indexOf('#') > 0)
{
QueryParser qp = new QueryParser(ejbql);
Object[] newArgs = args.clone();
newArgs[0] = qp.getEjbql();
Query query = (Query) method.invoke(getDelegate(method), newArgs);
for (int i = 0; i < qp.getParameterValueBindings().size(); i++)
{
query.setParameter(QueryParser.getParameterName(i), qp.getParameterValueBindings().get(i).getValue());
}
return query;
}
else
{
return method.invoke(getDelegate(method), args);
}
}
protected Object handleReconnectNoArg(Method method) throws Throwable
{
throw new UnsupportedOperationException("deprecated");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment