[jboss-cvs] JBossAS SVN: r61628 - in trunk/ejb3/src: main/org/jboss/ejb3/entity/hibernate and 3 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Mar 22 21:41:21 EDT 2007
Author: epbernard
Date: 2007-03-22 21:41:20 -0400 (Thu, 22 Mar 2007)
New Revision: 61628
Added:
trunk/ejb3/src/main/org/jboss/ejb3/entity/hibernate/
trunk/ejb3/src/main/org/jboss/ejb3/entity/hibernate/ExtendedSessionInvocationHandler.java
trunk/ejb3/src/main/org/jboss/ejb3/entity/hibernate/TransactionScopedSessionInvocationHandler.java
Removed:
trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedHibernateSession.java
trunk/ejb3/src/main/org/jboss/ejb3/entity/TransactionScopedHibernateSession.java
Modified:
trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedEntityManager.java
trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedPersistenceContext.java
trunk/ejb3/src/main/org/jboss/injection/PcEncInjector.java
trunk/ejb3/src/test/org/jboss/ejb3/test/entity/EntityTest.java
trunk/ejb3/src/test/org/jboss/ejb3/test/entity/EntityTestBean.java
trunk/ejb3/src/test/org/jboss/ejb3/test/entity/unit/EntityUnitTestCase.java
Log:
EJBTHREE-714 use a Proxy based approach to implement the Hibernate session wrappers
Modified: trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedEntityManager.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedEntityManager.java 2007-03-23 01:29:05 UTC (rev 61627)
+++ trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedEntityManager.java 2007-03-23 01:41:20 UTC (rev 61628)
@@ -48,11 +48,6 @@
{
}
- public ManagedEntityManagerFactory getFactory()
- {
- throw new RuntimeException("NOT IMPLEMENTED");
- }
-
public EntityManager getPersistenceContext()
{
StatefulBeanContext beanContext = StatefulBeanContext.currentBean.get();
Deleted: trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedHibernateSession.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedHibernateSession.java 2007-03-23 01:29:05 UTC (rev 61627)
+++ trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedHibernateSession.java 2007-03-23 01:41:20 UTC (rev 61628)
@@ -1,458 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2006, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.ejb3.entity;
-
-import java.io.Serializable;
-import java.sql.Connection;
-import javax.persistence.EntityManager;
-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.LockMode;
-import org.hibernate.Query;
-import org.hibernate.ReplicationMode;
-import org.hibernate.SQLQuery;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.Transaction;
-import org.hibernate.ejb.HibernateEntityManager;
-import org.hibernate.stat.SessionStatistics;
-import org.jboss.ejb3.stateful.StatefulBeanContext;
-
-/**
- * Hibernate Session with a managed extended persistence context.
- *
- * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
- */
-public class ExtendedHibernateSession implements Session, ExtendedPersistenceContext, Serializable
-{
- private String identity;
-
- public ExtendedHibernateSession(String name)
- {
- this.identity = name;
- }
-
- public ExtendedHibernateSession()
- {
- }
-
- public ManagedEntityManagerFactory getFactory()
- {
- throw new RuntimeException("NOT IMPLEMENTED");
- }
-
- public EntityManager getPersistenceContext()
- {
- StatefulBeanContext beanContext = StatefulBeanContext.currentBean.get();
- EntityManager persistenceContext = beanContext.getExtendedPersistenceContext(identity);
- if (persistenceContext == null)
- throw new RuntimeException("Unable to determine persistenceContext: " + identity
- + " in injected SFSB: " + beanContext.getContainer().getObjectName());
- return persistenceContext;
- }
-
- public Session getHibernateSession()
- {
- EntityManager persistenceContext = getPersistenceContext();
- if (persistenceContext instanceof HibernateEntityManager)
- {
- return ((HibernateEntityManager) persistenceContext).getSession();
- }
- throw new RuntimeException("ILLEGAL ACTION: Not a Hibernate persistence provider");
- }
-
-
- public EntityMode getEntityMode()
- {
- return getHibernateSession().getEntityMode();
- }
-
- public Session getSession(EntityMode entityMode)
- {
- return getHibernateSession().getSession(entityMode);
- }
-
- public void flush()
- throws HibernateException
- {
- getHibernateSession().flush();
- }
-
- public void setFlushMode(FlushMode flushMode)
- {
- getHibernateSession().setFlushMode(flushMode);
- }
-
- public FlushMode getFlushMode()
- {
- return getHibernateSession().getFlushMode();
- }
-
- public void setCacheMode(CacheMode cacheMode)
- {
- getHibernateSession().setCacheMode(cacheMode);
- }
-
- public CacheMode getCacheMode()
- {
- return getHibernateSession().getCacheMode();
- }
-
- public SessionFactory getSessionFactory()
- {
- return getHibernateSession().getSessionFactory();
- }
-
- public Connection connection()
- throws HibernateException
- {
- return getHibernateSession().connection();
- }
-
- public Connection disconnect()
- throws HibernateException
- {
- return getHibernateSession().disconnect();
- }
-
- public void reconnect()
- throws HibernateException
- {
- getHibernateSession().reconnect();
- }
-
- public void reconnect(Connection connection)
- throws HibernateException
- {
- getHibernateSession().reconnect(connection);
- }
-
- public Connection close()
- throws HibernateException
- {
- throw new IllegalStateException("It is illegal to close an injected Hibernate Session");
- }
-
- public void cancelQuery()
- throws HibernateException
- {
- getHibernateSession().cancelQuery();
- }
-
- public boolean isOpen()
- {
- return getHibernateSession().isOpen();
- }
-
- public boolean isConnected()
- {
- return getHibernateSession().isConnected();
- }
-
- public boolean isDirty()
- throws HibernateException
- {
- return getHibernateSession().isDirty();
- }
-
- public Serializable getIdentifier(Object object)
- throws HibernateException
- {
- return getHibernateSession().getIdentifier(object);
- }
-
- public boolean contains(Object object)
- {
- return getHibernateSession().contains(object);
- }
-
- public void evict(Object object)
- throws HibernateException
- {
- getHibernateSession().evict(object);
- }
-
- public Object load(Class theClass, Serializable id, LockMode lockMode)
- throws HibernateException
- {
- return getHibernateSession().load(theClass, id, lockMode);
- }
-
- public Object load(String entityName, Serializable id, LockMode lockMode)
- throws HibernateException
- {
- return getHibernateSession().load(entityName, id, lockMode);
- }
-
- public Object load(Class theClass, Serializable id)
- throws HibernateException
- {
- return getHibernateSession().load(theClass, id);
- }
-
- public Object load(String entityName, Serializable id)
- throws HibernateException
- {
- return getHibernateSession().load(entityName, id);
- }
-
- public void load(Object object, Serializable id)
- throws HibernateException
- {
- getHibernateSession().load(object, id);
- }
-
- public void replicate(Object object, ReplicationMode replicationMode)
- throws HibernateException
- {
- getHibernateSession().replicate(object, replicationMode);
- }
-
- public void replicate(String entityName, Object object, ReplicationMode replicationMode)
- throws HibernateException
- {
- getHibernateSession().replicate(entityName, object, replicationMode);
- }
-
- public Serializable save(Object object)
- throws HibernateException
- {
- return getHibernateSession().save(object);
- }
-
- public Serializable save(String entityName, Object object)
- throws HibernateException
- {
- return getHibernateSession().save(entityName, object);
- }
-
- public void saveOrUpdate(Object object)
- throws HibernateException
- {
- getHibernateSession().saveOrUpdate(object);
- }
-
- public void saveOrUpdate(String entityName, Object object)
- throws HibernateException
- {
- getHibernateSession().saveOrUpdate(entityName, object);
- }
-
- public void update(Object object)
- throws HibernateException
- {
- getHibernateSession().update(object);
- }
-
- public void update(String entityName, Object object)
- throws HibernateException
- {
- getHibernateSession().update(entityName, object);
- }
-
- public Object merge(Object object)
- throws HibernateException
- {
- return getHibernateSession().merge(object);
- }
-
- public Object merge(String entityName, Object object)
- throws HibernateException
- {
- return getHibernateSession().merge(entityName, object);
- }
-
- public void persist(Object object)
- throws HibernateException
- {
- getHibernateSession().persist(object);
- }
-
- public void persist(String entityName, Object object)
- throws HibernateException
- {
- getHibernateSession().persist(entityName, object);
- }
-
- public void delete(Object object)
- throws HibernateException
- {
- getHibernateSession().delete(object);
- }
-
- public void delete(String entityName, Object object)
- throws HibernateException
- {
- getHibernateSession().delete(entityName, object);
- }
-
- public void lock(Object object, LockMode lockMode)
- throws HibernateException
- {
- getHibernateSession().lock(object, lockMode);
- }
-
- public void lock(String entityName, Object object, LockMode lockMode)
- throws HibernateException
- {
- getHibernateSession().lock(entityName, object, lockMode);
- }
-
- public void refresh(Object object)
- throws HibernateException
- {
- getHibernateSession().refresh(object);
- }
-
- public void refresh(Object object, LockMode lockMode)
- throws HibernateException
- {
- getHibernateSession().refresh(object, lockMode);
- }
-
- public LockMode getCurrentLockMode(Object object)
- throws HibernateException
- {
- return getHibernateSession().getCurrentLockMode(object);
- }
-
- public Transaction beginTransaction()
- throws HibernateException
- {
- return getHibernateSession().beginTransaction();
- }
-
- public Criteria createCriteria(Class persistentClass)
- {
- return getHibernateSession().createCriteria(persistentClass);
- }
-
- public Criteria createCriteria(Class persistentClass, String alias)
- {
- return getHibernateSession().createCriteria(persistentClass, alias);
- }
-
- public Criteria createCriteria(String entityName)
- {
- return getHibernateSession().createCriteria(entityName);
- }
-
- public Criteria createCriteria(String entityName, String alias)
- {
- return getHibernateSession().createCriteria(entityName, alias);
- }
-
- public Query createQuery(String queryString)
- throws HibernateException
- {
- return getHibernateSession().createQuery(queryString);
- }
-
- public SQLQuery createSQLQuery(String queryString)
- throws HibernateException
- {
- return getHibernateSession().createSQLQuery(queryString);
- }
-
- public Query createFilter(Object collection, String queryString)
- throws HibernateException
- {
- return getHibernateSession().createFilter(collection, queryString);
- }
-
- public Query getNamedQuery(String queryName)
- throws HibernateException
- {
- return getHibernateSession().getNamedQuery(queryName);
- }
-
- public void clear()
- {
- getHibernateSession().clear();
- }
-
- public Object get(Class clazz, Serializable id)
- throws HibernateException
- {
- return getHibernateSession().get(clazz, id);
- }
-
- public Object get(Class clazz, Serializable id, LockMode lockMode)
- throws HibernateException
- {
- return getHibernateSession().get(clazz, id, lockMode);
- }
-
- public Object get(String entityName, Serializable id)
- throws HibernateException
- {
- return getHibernateSession().get(entityName, id);
- }
-
- public Object get(String entityName, Serializable id, LockMode lockMode)
- throws HibernateException
- {
- return getHibernateSession().get(entityName, id, lockMode);
- }
-
- public String getEntityName(Object object)
- throws HibernateException
- {
- return getHibernateSession().getEntityName(object);
- }
-
- public Filter enableFilter(String filterName)
- {
- return getHibernateSession().enableFilter(filterName);
- }
-
- public Filter getEnabledFilter(String filterName)
- {
- return getHibernateSession().getEnabledFilter(filterName);
- }
-
- public void disableFilter(String filterName)
- {
- getHibernateSession().disableFilter(filterName);
- }
-
- public SessionStatistics getStatistics()
- {
- return getHibernateSession().getStatistics();
- }
-
- public void setReadOnly(Object entity, boolean readOnly)
- {
- getHibernateSession().setReadOnly(entity, readOnly);
- }
-
- public Transaction getTransaction()
- {
- return getHibernateSession().getTransaction();
- }
-
-}
-
Modified: trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedPersistenceContext.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedPersistenceContext.java 2007-03-23 01:29:05 UTC (rev 61627)
+++ trunk/ejb3/src/main/org/jboss/ejb3/entity/ExtendedPersistenceContext.java 2007-03-23 01:41:20 UTC (rev 61628)
@@ -31,7 +31,5 @@
*/
public interface ExtendedPersistenceContext
{
- ManagedEntityManagerFactory getFactory();
-
EntityManager getPersistenceContext();
}
Deleted: trunk/ejb3/src/main/org/jboss/ejb3/entity/TransactionScopedHibernateSession.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/entity/TransactionScopedHibernateSession.java 2007-03-23 01:29:05 UTC (rev 61627)
+++ trunk/ejb3/src/main/org/jboss/ejb3/entity/TransactionScopedHibernateSession.java 2007-03-23 01:41:20 UTC (rev 61628)
@@ -1,463 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2006, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.ejb3.entity;
-
-import java.io.Externalizable;
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-import java.io.Serializable;
-import java.sql.Connection;
-import javax.persistence.EntityManager;
-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.LockMode;
-import org.hibernate.ReplicationMode;
-import org.hibernate.SQLQuery;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.Transaction;
-import org.hibernate.ejb.HibernateEntityManager;
-import org.hibernate.stat.SessionStatistics;
-import org.jboss.ejb3.PersistenceUnitRegistry;
-
-
-/**
- * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
- * @version $Revision$
- */
-public class TransactionScopedHibernateSession implements Session, Externalizable
-{
- private transient ManagedEntityManagerFactory factory;
-
- protected Session getHibernateSession()
- {
- if (getSession() instanceof HibernateEntityManager)
- {
- return ((HibernateEntityManager) getSession()).getSession();
- }
- throw new RuntimeException("ILLEGAL ACTION: Not a Hibernate pe" +
- "rsistence provider");
- }
-
- protected EntityManager getSession()
- {
- return factory.getTransactionScopedEntityManager();
- }
-
- public TransactionScopedHibernateSession(ManagedEntityManagerFactory factory)
- {
- if (factory == null) throw new NullPointerException("factory must not be null");
- this.factory = factory;
- }
-
- public TransactionScopedHibernateSession()
- {
-
- }
-
- public void writeExternal(ObjectOutput out) throws IOException
- {
- out.writeUTF(factory.getKernelName());
- }
-
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
- {
- String kernelName = in.readUTF();
- PersistenceUnitDeployment deployment = PersistenceUnitRegistry.getPersistenceUnit(kernelName);
- if (deployment == null) throw new IOException("Unable to find persistence unit in registry: " + kernelName);
- factory = deployment.getManagedFactory();
- }
-
- public EntityMode getEntityMode()
- {
- return getHibernateSession().getEntityMode();
- }
-
- public Session getSession(EntityMode entityMode)
- {
- return getHibernateSession().getSession(entityMode);
- }
-
- public void flush()
- throws HibernateException
- {
- getHibernateSession().flush();
- }
-
- public void setFlushMode(FlushMode flushMode)
- {
- getHibernateSession().setFlushMode(flushMode);
- }
-
- public FlushMode getFlushMode()
- {
- return getHibernateSession().getFlushMode();
- }
-
- public void setCacheMode(CacheMode cacheMode)
- {
- getHibernateSession().setCacheMode(cacheMode);
- }
-
- public CacheMode getCacheMode()
- {
- return getHibernateSession().getCacheMode();
- }
-
- public SessionFactory getSessionFactory()
- {
- return getHibernateSession().getSessionFactory();
- }
-
- public Connection connection()
- throws HibernateException
- {
- return getHibernateSession().connection();
- }
-
- public Connection disconnect()
- throws HibernateException
- {
- return getHibernateSession().disconnect();
- }
-
- public void reconnect()
- throws HibernateException
- {
- getHibernateSession().reconnect();
- }
-
- public void reconnect(Connection connection)
- throws HibernateException
- {
- getHibernateSession().reconnect(connection);
- }
-
- public Connection close()
- throws HibernateException
- {
- throw new IllegalStateException("Illegal to call this method from injected, managed EntityManager");
- }
-
- public void cancelQuery()
- throws HibernateException
- {
- getHibernateSession().cancelQuery();
- }
-
- public boolean isOpen()
- {
- return getHibernateSession().isOpen();
- }
-
- public boolean isConnected()
- {
- return getHibernateSession().isConnected();
- }
-
- public boolean isDirty()
- throws HibernateException
- {
- return getHibernateSession().isDirty();
- }
-
- public Serializable getIdentifier(Object object)
- throws HibernateException
- {
- return getHibernateSession().getIdentifier(object);
- }
-
- public boolean contains(Object object)
- {
- return getHibernateSession().contains(object);
- }
-
- public void evict(Object object)
- throws HibernateException
- {
- getHibernateSession().evict(object);
- }
-
- public Object load(Class theClass, Serializable id, LockMode lockMode)
- throws HibernateException
- {
- return getHibernateSession().load(theClass, id, lockMode);
- }
-
- public Object load(String entityName, Serializable id, LockMode lockMode)
- throws HibernateException
- {
- return getHibernateSession().load(entityName, id, lockMode);
- }
-
- public Object load(Class theClass, Serializable id)
- throws HibernateException
- {
- return getHibernateSession().load(theClass, id);
- }
-
- public Object load(String entityName, Serializable id)
- throws HibernateException
- {
- return getHibernateSession().load(entityName, id);
- }
-
- public void load(Object object, Serializable id)
- throws HibernateException
- {
- getHibernateSession().load(object, id);
- }
-
- public void replicate(Object object, ReplicationMode replicationMode)
- throws HibernateException
- {
- getHibernateSession().replicate(object, replicationMode);
- }
-
- public void replicate(String entityName, Object object, ReplicationMode replicationMode)
- throws HibernateException
- {
- getHibernateSession().replicate(entityName, object, replicationMode);
- }
-
- public Serializable save(Object object)
- throws HibernateException
- {
- return getHibernateSession().save(object);
- }
-
- public Serializable save(String entityName, Object object)
- throws HibernateException
- {
- return getHibernateSession().save(entityName, object);
- }
-
- public void saveOrUpdate(Object object)
- throws HibernateException
- {
- getHibernateSession().saveOrUpdate(object);
- }
-
- public void saveOrUpdate(String entityName, Object object)
- throws HibernateException
- {
- getHibernateSession().saveOrUpdate(entityName, object);
- }
-
- public void update(Object object)
- throws HibernateException
- {
- getHibernateSession().update(object);
- }
-
- public void update(String entityName, Object object)
- throws HibernateException
- {
- getHibernateSession().update(entityName, object);
- }
-
- public Object merge(Object object)
- throws HibernateException
- {
- return getHibernateSession().merge(object);
- }
-
- public Object merge(String entityName, Object object)
- throws HibernateException
- {
- return getHibernateSession().merge(entityName, object);
- }
-
- public void persist(Object object)
- throws HibernateException
- {
- getHibernateSession().persist(object);
- }
-
- public void persist(String entityName, Object object)
- throws HibernateException
- {
- getHibernateSession().persist(entityName, object);
- }
-
- public void delete(Object object)
- throws HibernateException
- {
- getHibernateSession().delete(object);
- }
-
- public void delete(String entityName, Object object)
- throws HibernateException
- {
- getHibernateSession().delete(entityName, object);
- }
-
- public void lock(Object object, LockMode lockMode)
- throws HibernateException
- {
- getHibernateSession().lock(object, lockMode);
- }
-
- public void lock(String entityName, Object object, LockMode lockMode)
- throws HibernateException
- {
- getHibernateSession().lock(entityName, object, lockMode);
- }
-
- public void refresh(Object object)
- throws HibernateException
- {
- getHibernateSession().refresh(object);
- }
-
- public void refresh(Object object, LockMode lockMode)
- throws HibernateException
- {
- getHibernateSession().refresh(object, lockMode);
- }
-
- public LockMode getCurrentLockMode(Object object)
- throws HibernateException
- {
- return getHibernateSession().getCurrentLockMode(object);
- }
-
- public Transaction beginTransaction()
- throws HibernateException
- {
- return getHibernateSession().beginTransaction();
- }
-
- public Criteria createCriteria(Class persistentClass)
- {
- return getHibernateSession().createCriteria(persistentClass);
- }
-
- public Criteria createCriteria(Class persistentClass, String alias)
- {
- return getHibernateSession().createCriteria(persistentClass, alias);
- }
-
- public Criteria createCriteria(String entityName)
- {
- return getHibernateSession().createCriteria(entityName);
- }
-
- public Criteria createCriteria(String entityName, String alias)
- {
- return getHibernateSession().createCriteria(entityName, alias);
- }
-
- public org.hibernate.Query createQuery(String queryString)
- throws HibernateException
- {
- return getHibernateSession().createQuery(queryString);
- }
-
- public SQLQuery createSQLQuery(String queryString)
- throws HibernateException
- {
- return getHibernateSession().createSQLQuery(queryString);
- }
-
- public org.hibernate.Query createFilter(Object collection, String queryString)
- throws HibernateException
- {
- return getHibernateSession().createFilter(collection, queryString);
- }
-
- public org.hibernate.Query getNamedQuery(String queryName)
- throws HibernateException
- {
- return getHibernateSession().getNamedQuery(queryName);
- }
-
- public void clear()
- {
- getHibernateSession().clear();
- }
-
- public Object get(Class clazz, Serializable id)
- throws HibernateException
- {
- return getHibernateSession().get(clazz, id);
- }
-
- public Object get(Class clazz, Serializable id, LockMode lockMode)
- throws HibernateException
- {
- return getHibernateSession().get(clazz, id, lockMode);
- }
-
- public Object get(String entityName, Serializable id)
- throws HibernateException
- {
- return getHibernateSession().get(entityName, id);
- }
-
- public Object get(String entityName, Serializable id, LockMode lockMode)
- throws HibernateException
- {
- return getHibernateSession().get(entityName, id, lockMode);
- }
-
- public String getEntityName(Object object)
- throws HibernateException
- {
- return getHibernateSession().getEntityName(object);
- }
-
- public Filter enableFilter(String filterName)
- {
- return getHibernateSession().enableFilter(filterName);
- }
-
- public Filter getEnabledFilter(String filterName)
- {
- return getHibernateSession().getEnabledFilter(filterName);
- }
-
- public void disableFilter(String filterName)
- {
- getHibernateSession().disableFilter(filterName);
- }
-
- public SessionStatistics getStatistics()
- {
- return getHibernateSession().getStatistics();
- }
-
- public void setReadOnly(Object entity, boolean readOnly)
- {
- getHibernateSession().setReadOnly(entity, readOnly);
- }
-
- public Transaction getTransaction()
- {
- return getHibernateSession().getTransaction();
- }
-}
Added: trunk/ejb3/src/main/org/jboss/ejb3/entity/hibernate/ExtendedSessionInvocationHandler.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/entity/hibernate/ExtendedSessionInvocationHandler.java (rev 0)
+++ trunk/ejb3/src/main/org/jboss/ejb3/entity/hibernate/ExtendedSessionInvocationHandler.java 2007-03-23 01:41:20 UTC (rev 61628)
@@ -0,0 +1,98 @@
+//$Id: $
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.entity.hibernate;
+
+import java.io.Serializable;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+import javax.persistence.EntityManager;
+
+import org.hibernate.Session;
+import org.hibernate.ejb.HibernateEntityManager;
+import org.jboss.ejb3.stateful.StatefulBeanContext;
+
+/**
+ * Handle method execution delegation to an Hibernate Session following the extended persistence context rules
+ *
+ * @author Emmanuel Bernard
+ */
+public class ExtendedSessionInvocationHandler implements InvocationHandler, Serializable
+{
+ private String identity;
+
+ public ExtendedSessionInvocationHandler(String identity)
+ {
+ this.identity = identity;
+ }
+
+ //is it useful?
+ public ExtendedSessionInvocationHandler()
+ {
+ }
+
+ public EntityManager getPersistenceContext()
+ {
+ StatefulBeanContext beanContext = StatefulBeanContext.currentBean.get();
+ EntityManager persistenceContext = beanContext.getExtendedPersistenceContext(identity);
+ if (persistenceContext == null)
+ throw new RuntimeException("Unable to determine persistenceContext: " + identity
+ + " in injected SFSB: " + beanContext.getContainer().getObjectName());
+ return persistenceContext;
+ }
+
+ public Session getHibernateSession()
+ {
+ EntityManager persistenceContext = getPersistenceContext();
+ if (persistenceContext instanceof HibernateEntityManager )
+ {
+ return ((HibernateEntityManager) persistenceContext).getSession();
+ }
+ throw new RuntimeException("ILLEGAL ACTION: Not a Hibernate persistence provider");
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ String methodName = method.getName();
+ if ( "getPersistenceContext".equals( methodName ) ) {
+ return getPersistenceContext();
+ }
+ else if ( "close".equals( methodName ) ) {
+ throw new IllegalStateException("It is illegal to close an injected Hibernate Session");
+ }
+ else {
+ //catch all
+ try {
+ return method.invoke( getHibernateSession(), args );
+ }
+ catch ( InvocationTargetException e ) {
+ if ( e.getTargetException() instanceof RuntimeException ) {
+ throw ( RuntimeException ) e.getTargetException();
+ }
+ else {
+ throw e;
+ }
+ }
+ }
+ }
+}
Added: trunk/ejb3/src/main/org/jboss/ejb3/entity/hibernate/TransactionScopedSessionInvocationHandler.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/ejb3/entity/hibernate/TransactionScopedSessionInvocationHandler.java (rev 0)
+++ trunk/ejb3/src/main/org/jboss/ejb3/entity/hibernate/TransactionScopedSessionInvocationHandler.java 2007-03-23 01:41:20 UTC (rev 61628)
@@ -0,0 +1,108 @@
+//$Id: $
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2007, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.entity.hibernate;
+
+import java.io.ObjectOutput;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.Externalizable;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+import javax.persistence.EntityManager;
+
+import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
+import org.jboss.ejb3.entity.PersistenceUnitDeployment;
+import org.jboss.ejb3.PersistenceUnitRegistry;
+import org.hibernate.Session;
+import org.hibernate.ejb.HibernateEntityManager;
+
+/**
+ * Handle method execution delegation to an Hibernate session following the transaction scoped persistence context rules
+ *
+ * @author Emmanuel Bernard
+ */
+public class TransactionScopedSessionInvocationHandler implements InvocationHandler, Externalizable
+{
+ private transient ManagedEntityManagerFactory factory;
+
+ public TransactionScopedSessionInvocationHandler(ManagedEntityManagerFactory factory)
+ {
+ if ( factory == null ) throw new NullPointerException( "factory must not be null" );
+ this.factory = factory;
+ }
+
+ //is it useful?
+ public TransactionScopedSessionInvocationHandler()
+ {
+ }
+
+ protected Session getHibernateSession()
+ {
+ if ( getSession() instanceof HibernateEntityManager )
+ {
+ return ( (HibernateEntityManager) getSession() ).getSession();
+ }
+ throw new RuntimeException( "ILLEGAL ACTION: Not a Hibernate persistence provider" );
+ }
+
+ protected EntityManager getSession()
+ {
+ return factory.getTransactionScopedEntityManager();
+ }
+
+
+ public void writeExternal(ObjectOutput out) throws IOException
+ {
+ out.writeUTF( factory.getKernelName() );
+ }
+
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
+ {
+ String kernelName = in.readUTF();
+ PersistenceUnitDeployment deployment = PersistenceUnitRegistry.getPersistenceUnit( kernelName );
+ if ( deployment == null ) throw new IOException( "Unable to find persistence unit in registry: " + kernelName );
+ factory = deployment.getManagedFactory();
+ }
+
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
+ {
+ if ( "close".equals( method.getName() ) ) {
+ throw new IllegalStateException("Illegal to call this method from injected, managed EntityManager");
+ }
+ else {
+ //catch all
+ try {
+ return method.invoke( getHibernateSession(), args );
+ }
+ catch ( InvocationTargetException e ) {
+ if ( e.getTargetException() instanceof RuntimeException ) {
+ throw ( RuntimeException ) e.getTargetException();
+ }
+ else {
+ throw e;
+ }
+ }
+ }
+ }
+}
Modified: trunk/ejb3/src/main/org/jboss/injection/PcEncInjector.java
===================================================================
--- trunk/ejb3/src/main/org/jboss/injection/PcEncInjector.java 2007-03-23 01:29:05 UTC (rev 61627)
+++ trunk/ejb3/src/main/org/jboss/injection/PcEncInjector.java 2007-03-23 01:41:20 UTC (rev 61628)
@@ -21,13 +21,18 @@
*/
package org.jboss.injection;
+import java.lang.reflect.Proxy;
+
import org.jboss.ejb3.entity.ManagedEntityManagerFactory;
import org.jboss.ejb3.entity.ExtendedEntityManager;
import org.jboss.ejb3.entity.ExtendedHibernateSession;
import org.jboss.ejb3.entity.TransactionScopedEntityManager;
import org.jboss.ejb3.entity.TransactionScopedHibernateSession;
+import org.jboss.ejb3.entity.hibernate.ExtendedSessionInvocationHandler;
+import org.jboss.ejb3.entity.hibernate.TransactionScopedSessionInvocationHandler;
import org.jboss.ejb3.stateful.StatefulContainer;
import org.jboss.naming.Util;
+import org.hibernate.Session;
import javax.persistence.PersistenceContextType;
import javax.persistence.EntityManager;
@@ -42,6 +47,13 @@
*/
public class PcEncInjector implements EncInjector
{
+ private static final Class[] SESS_PROXY_INTERFACES = new Class[] {
+ org.hibernate.classic.Session.class,
+ org.hibernate.engine.SessionImplementor.class,
+ org.hibernate.jdbc.JDBCContext.Context.class,
+ org.hibernate.event.EventSource.class
+ };
+
private String encName;
private String unitName;
private PersistenceContextType type;
@@ -79,7 +91,7 @@
if (!(container instanceof StatefulContainer))
throw new RuntimeException("It is illegal to inject an EXTENDED PC into something other than a SFSB");
container.getInjectors().add(0, new ExtendedPersistenceContextInjector(factory));
- Object extendedPc = null;
+ Object extendedPc;
if (injectionType == null
|| injectionType.getName().equals(EntityManager.class.getName()))
{
@@ -87,7 +99,13 @@
}
else
{
- extendedPc = new ExtendedHibernateSession(factory.getKernelName());
+ ExtendedSessionInvocationHandler handler = new ExtendedSessionInvocationHandler(factory.getKernelName());
+ extendedPc = Proxy.newProxyInstance(
+ Session.class.getClassLoader(), //use the Hibernate classloader so the proxy has the same scope as Hibernate
+ SESS_PROXY_INTERFACES,
+ handler
+ );
+ extendedPc = new ExtendedHibernateSession( factory.getKernelName() );
}
try
{
@@ -100,7 +118,7 @@
}
else
{
- Object entityManager = null;
+ Object entityManager;
if (injectionType == null
|| injectionType.getName().equals(EntityManager.class.getName()))
{
@@ -108,7 +126,13 @@
}
else
{
- entityManager = new TransactionScopedHibernateSession(factory);
+ TransactionScopedSessionInvocationHandler handler = new TransactionScopedSessionInvocationHandler(factory);
+ entityManager = Proxy.newProxyInstance(
+ Session.class.getClassLoader(), //use the Hibernate classloader so the proxy has the same scope as Hibernate
+ SESS_PROXY_INTERFACES,
+ handler
+ );
+ entityManager = new TransactionScopedHibernateSession( factory );
}
try
{
Modified: trunk/ejb3/src/test/org/jboss/ejb3/test/entity/EntityTest.java
===================================================================
--- trunk/ejb3/src/test/org/jboss/ejb3/test/entity/EntityTest.java 2007-03-23 01:29:05 UTC (rev 61627)
+++ trunk/ejb3/src/test/org/jboss/ejb3/test/entity/EntityTest.java 2007-03-23 01:41:20 UTC (rev 61628)
@@ -69,4 +69,6 @@
Customer loadCustomer(Long id);
boolean isDelegateASession();
+
+ public boolean isTrueHibernateSession();
}
Modified: trunk/ejb3/src/test/org/jboss/ejb3/test/entity/EntityTestBean.java
===================================================================
--- trunk/ejb3/src/test/org/jboss/ejb3/test/entity/EntityTestBean.java 2007-03-23 01:29:05 UTC (rev 61627)
+++ trunk/ejb3/src/test/org/jboss/ejb3/test/entity/EntityTestBean.java 2007-03-23 01:41:20 UTC (rev 61628)
@@ -22,6 +22,9 @@
package org.jboss.ejb3.test.entity;
import org.hibernate.Session;
+import org.hibernate.jdbc.JDBCContext;
+import org.hibernate.event.EventSource;
+import org.hibernate.engine.SessionImplementor;
import org.jboss.annotation.JndiInject;
import java.util.ArrayList;
@@ -292,6 +295,15 @@
}
public boolean isDelegateASession() {
+ //has to delegate to the underlying entitymanager
return (manager.getDelegate() != null) && (manager.getDelegate() instanceof Session);
}
+
+ public boolean isTrueHibernateSession() {
+ //has to implement the private Session interfaces
+ return (session instanceof Session)
+ && (session instanceof SessionImplementor)
+ && (session instanceof EventSource)
+ && (session instanceof JDBCContext.Context);
+ }
}
Modified: trunk/ejb3/src/test/org/jboss/ejb3/test/entity/unit/EntityUnitTestCase.java
===================================================================
--- trunk/ejb3/src/test/org/jboss/ejb3/test/entity/unit/EntityUnitTestCase.java 2007-03-23 01:29:05 UTC (rev 61627)
+++ trunk/ejb3/src/test/org/jboss/ejb3/test/entity/unit/EntityUnitTestCase.java 2007-03-23 01:41:20 UTC (rev 61628)
@@ -208,6 +208,12 @@
assertTrue( "delegate is not an hibernate Session", test.isDelegateASession() );
}
+ public void testTrueHibernateSession() throws Exception
+ {
+ EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote");
+ assertTrue( "Sesison object does not implement the private session interfaces", test.isTrueHibernateSession() );
+ }
+
public static Test suite() throws Exception
{
return getDeploySetup(EntityUnitTestCase.class, "entity-test.jar");
More information about the jboss-cvs-commits
mailing list