1 22 package org.jboss.ejb3.entity; 23 24 import java.io.Externalizable ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 import java.io.Serializable ; 29 import java.sql.Connection ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import javax.naming.NamingException ; 33 import javax.naming.Reference ; 34 import javax.persistence.EntityManagerFactory; 35 import org.hibernate.HibernateException; 36 import org.hibernate.Interceptor; 37 import org.hibernate.SessionFactory; 38 import org.hibernate.StatelessSession; 39 import org.hibernate.classic.Session; 40 import org.hibernate.ejb.HibernateEntityManagerFactory; 41 import org.hibernate.engine.FilterDefinition; 42 import org.hibernate.metadata.ClassMetadata; 43 import org.hibernate.metadata.CollectionMetadata; 44 import org.hibernate.stat.Statistics; 45 import org.jboss.ejb3.PersistenceUnitRegistry; 46 47 53 public class InjectedSessionFactory implements SessionFactory, Externalizable 54 { 55 private transient EntityManagerFactory delegate; 56 private transient ManagedEntityManagerFactory managedFactory; 57 58 public InjectedSessionFactory(ManagedEntityManagerFactory factory) 59 { 60 this.managedFactory = factory; 61 this.delegate = factory.getEntityManagerFactory(); 62 } 63 64 public InjectedSessionFactory() {} 65 66 public void writeExternal(ObjectOutput out) throws IOException 67 { 68 out.writeUTF(managedFactory.getKernelName()); 69 } 70 71 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 72 { 73 String kernelName = in.readUTF(); 74 PersistenceUnitDeployment deployment = PersistenceUnitRegistry.getPersistenceUnit(kernelName); 75 if (deployment == null) throw new IOException ("Unable to find persistence unit in registry: " + kernelName); 76 managedFactory = deployment.getManagedFactory(); 77 delegate = managedFactory.getEntityManagerFactory(); 78 } 79 80 private EntityManagerFactory getDelegate() 81 { 82 return delegate; 83 } 84 85 private SessionFactory getSessionFactory() 86 { 87 return ((HibernateEntityManagerFactory)getDelegate()).getSessionFactory(); 88 } 89 90 public Set getDefinedFilterNames() 91 { 92 return getSessionFactory().getDefinedFilterNames(); 93 } 94 95 public FilterDefinition getFilterDefinition(String filterName) throws HibernateException 96 { 97 return getSessionFactory().getFilterDefinition(filterName); 98 } 99 100 public Session openSession(Connection connection) 101 { 102 return getSessionFactory().openSession(connection); 103 } 104 105 public Session openSession(Interceptor interceptor) 106 throws HibernateException 107 { 108 return getSessionFactory().openSession(interceptor); 109 } 110 111 public Session openSession(Connection connection, Interceptor interceptor) 112 { 113 return getSessionFactory().openSession(connection, interceptor); 114 } 115 116 public Session openSession() 117 throws HibernateException 118 { 119 return getSessionFactory().openSession(); 120 } 121 122 public Session getCurrentSession() 123 throws HibernateException 124 { 125 return getSessionFactory().getCurrentSession(); 126 } 127 128 public ClassMetadata getClassMetadata(Class persistentClass) 129 throws HibernateException 130 { 131 return getSessionFactory().getClassMetadata(persistentClass); 132 } 133 134 public ClassMetadata getClassMetadata(String entityName) 135 throws HibernateException 136 { 137 return getSessionFactory().getClassMetadata(entityName); 138 } 139 140 public CollectionMetadata getCollectionMetadata(String roleName) 141 throws HibernateException 142 { 143 return getSessionFactory().getCollectionMetadata(roleName); 144 } 145 146 public Map getAllClassMetadata() 147 throws HibernateException 148 { 149 return getSessionFactory().getAllClassMetadata(); 150 } 151 152 public Map getAllCollectionMetadata() 153 throws HibernateException 154 { 155 return getSessionFactory().getAllCollectionMetadata(); 156 } 157 158 public Statistics getStatistics() 159 { 160 return getSessionFactory().getStatistics(); 161 } 162 163 public void close() 164 throws HibernateException 165 { 166 throw new IllegalStateException ("It is illegal to close an injected SessionFactory"); 167 } 168 169 public boolean isClosed() 170 { 171 return getSessionFactory().isClosed(); 172 } 173 174 public void evict(Class persistentClass) 175 throws HibernateException 176 { 177 getSessionFactory().evict(persistentClass); 178 } 179 180 public void evict(Class persistentClass, Serializable id) 181 throws HibernateException 182 { 183 getSessionFactory().evict(persistentClass, id); 184 } 185 186 public void evictEntity(String entityName) 187 throws HibernateException 188 { 189 getSessionFactory().evictEntity(entityName); 190 } 191 192 public void evictEntity(String entityName, Serializable id) 193 throws HibernateException 194 { 195 getSessionFactory().evictEntity(entityName, id); 196 } 197 198 public void evictCollection(String roleName) 199 throws HibernateException 200 { 201 getSessionFactory().evictCollection(roleName); 202 } 203 204 public void evictCollection(String roleName, Serializable id) 205 throws HibernateException 206 { 207 getSessionFactory().evictCollection(roleName, id); 208 } 209 210 public void evictQueries() 211 throws HibernateException 212 { 213 getSessionFactory().evictQueries(); 214 } 215 216 public void evictQueries(String cacheRegion) 217 throws HibernateException 218 { 219 getSessionFactory().evictQueries(cacheRegion); 220 } 221 222 public StatelessSession openStatelessSession() 223 { 224 return getSessionFactory().openStatelessSession(); 225 } 226 227 public StatelessSession openStatelessSession(Connection connection) 228 { 229 return getSessionFactory().openStatelessSession(connection); 230 } 231 232 public Reference getReference() 233 throws NamingException 234 { 235 return getSessionFactory().getReference(); 236 } 237 238 } 239 | Popular Tags |