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 javax.persistence.EntityManager; 31 import org.hibernate.CacheMode; 32 import org.hibernate.Criteria; 33 import org.hibernate.EntityMode; 34 import org.hibernate.Filter; 35 import org.hibernate.FlushMode; 36 import org.hibernate.HibernateException; 37 import org.hibernate.LockMode; 38 import org.hibernate.ReplicationMode; 39 import org.hibernate.SQLQuery; 40 import org.hibernate.Session; 41 import org.hibernate.SessionFactory; 42 import org.hibernate.Transaction; 43 import org.hibernate.ejb.HibernateEntityManager; 44 import org.hibernate.stat.SessionStatistics; 45 import org.jboss.ejb3.PersistenceUnitRegistry; 46 47 48 52 public class TransactionScopedHibernateSession implements Session, Externalizable 53 { 54 private transient ManagedEntityManagerFactory factory; 55 56 protected Session getHibernateSession() 57 { 58 if (getSession() instanceof HibernateEntityManager) 59 { 60 return ((HibernateEntityManager) getSession()).getSession(); 61 } 62 throw new RuntimeException ("ILLEGAL ACTION: Not a Hibernate pe" + 63 "rsistence provider"); 64 } 65 66 protected EntityManager getSession() 67 { 68 return factory.getTransactionScopedEntityManager(); 69 } 70 71 public TransactionScopedHibernateSession(ManagedEntityManagerFactory factory) 72 { 73 if (factory == null) throw new NullPointerException ("factory must not be null"); 74 this.factory = factory; 75 } 76 77 public TransactionScopedHibernateSession() 78 { 79 80 } 81 82 public void writeExternal(ObjectOutput out) throws IOException 83 { 84 out.writeUTF(factory.getKernelName()); 85 } 86 87 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 88 { 89 String kernelName = in.readUTF(); 90 PersistenceUnitDeployment deployment = PersistenceUnitRegistry.getPersistenceUnit(kernelName); 91 if (deployment == null) throw new IOException ("Unable to find persistence unit in registry: " + kernelName); 92 factory = deployment.getManagedFactory(); 93 } 94 95 public EntityMode getEntityMode() 96 { 97 return getHibernateSession().getEntityMode(); 98 } 99 100 public Session getSession(EntityMode entityMode) 101 { 102 return getHibernateSession().getSession(entityMode); 103 } 104 105 public void flush() 106 throws HibernateException 107 { 108 getHibernateSession().flush(); 109 } 110 111 public void setFlushMode(FlushMode flushMode) 112 { 113 getHibernateSession().setFlushMode(flushMode); 114 } 115 116 public FlushMode getFlushMode() 117 { 118 return getHibernateSession().getFlushMode(); 119 } 120 121 public void setCacheMode(CacheMode cacheMode) 122 { 123 getHibernateSession().setCacheMode(cacheMode); 124 } 125 126 public CacheMode getCacheMode() 127 { 128 return getHibernateSession().getCacheMode(); 129 } 130 131 public SessionFactory getSessionFactory() 132 { 133 return getHibernateSession().getSessionFactory(); 134 } 135 136 public Connection connection() 137 throws HibernateException 138 { 139 return getHibernateSession().connection(); 140 } 141 142 public Connection disconnect() 143 throws HibernateException 144 { 145 return getHibernateSession().disconnect(); 146 } 147 148 public void reconnect() 149 throws HibernateException 150 { 151 getHibernateSession().reconnect(); 152 } 153 154 public void reconnect(Connection connection) 155 throws HibernateException 156 { 157 getHibernateSession().reconnect(connection); 158 } 159 160 public Connection close() 161 throws HibernateException 162 { 163 throw new IllegalStateException ("Illegal to call this method from injected, managed EntityManager"); 164 } 165 166 public void cancelQuery() 167 throws HibernateException 168 { 169 getHibernateSession().cancelQuery(); 170 } 171 172 public boolean isOpen() 173 { 174 return getHibernateSession().isOpen(); 175 } 176 177 public boolean isConnected() 178 { 179 return getHibernateSession().isConnected(); 180 } 181 182 public boolean isDirty() 183 throws HibernateException 184 { 185 return getHibernateSession().isDirty(); 186 } 187 188 public Serializable getIdentifier(Object object) 189 throws HibernateException 190 { 191 return getHibernateSession().getIdentifier(object); 192 } 193 194 public boolean contains(Object object) 195 { 196 return getHibernateSession().contains(object); 197 } 198 199 public void evict(Object object) 200 throws HibernateException 201 { 202 getHibernateSession().evict(object); 203 } 204 205 public Object load(Class theClass, Serializable id, LockMode lockMode) 206 throws HibernateException 207 { 208 return getHibernateSession().load(theClass, id, lockMode); 209 } 210 211 public Object load(String entityName, Serializable id, LockMode lockMode) 212 throws HibernateException 213 { 214 return getHibernateSession().load(entityName, id, lockMode); 215 } 216 217 public Object load(Class theClass, Serializable id) 218 throws HibernateException 219 { 220 return getHibernateSession().load(theClass, id); 221 } 222 223 public Object load(String entityName, Serializable id) 224 throws HibernateException 225 { 226 return getHibernateSession().load(entityName, id); 227 } 228 229 public void load(Object object, Serializable id) 230 throws HibernateException 231 { 232 getHibernateSession().load(object, id); 233 } 234 235 public void replicate(Object object, ReplicationMode replicationMode) 236 throws HibernateException 237 { 238 getHibernateSession().replicate(object, replicationMode); 239 } 240 241 public void replicate(String entityName, Object object, ReplicationMode replicationMode) 242 throws HibernateException 243 { 244 getHibernateSession().replicate(entityName, object, replicationMode); 245 } 246 247 public Serializable save(Object object) 248 throws HibernateException 249 { 250 return getHibernateSession().save(object); 251 } 252 253 public Serializable save(String entityName, Object object) 254 throws HibernateException 255 { 256 return getHibernateSession().save(entityName, object); 257 } 258 259 public void saveOrUpdate(Object object) 260 throws HibernateException 261 { 262 getHibernateSession().saveOrUpdate(object); 263 } 264 265 public void saveOrUpdate(String entityName, Object object) 266 throws HibernateException 267 { 268 getHibernateSession().saveOrUpdate(entityName, object); 269 } 270 271 public void update(Object object) 272 throws HibernateException 273 { 274 getHibernateSession().update(object); 275 } 276 277 public void update(String entityName, Object object) 278 throws HibernateException 279 { 280 getHibernateSession().update(entityName, object); 281 } 282 283 public Object merge(Object object) 284 throws HibernateException 285 { 286 return getHibernateSession().merge(object); 287 } 288 289 public Object merge(String entityName, Object object) 290 throws HibernateException 291 { 292 return getHibernateSession().merge(entityName, object); 293 } 294 295 public void persist(Object object) 296 throws HibernateException 297 { 298 getHibernateSession().persist(object); 299 } 300 301 public void persist(String entityName, Object object) 302 throws HibernateException 303 { 304 getHibernateSession().persist(entityName, object); 305 } 306 307 public void delete(Object object) 308 throws HibernateException 309 { 310 getHibernateSession().delete(object); 311 } 312 313 public void delete(String entityName, Object object) 314 throws HibernateException 315 { 316 getHibernateSession().delete(entityName, object); 317 } 318 319 public void lock(Object object, LockMode lockMode) 320 throws HibernateException 321 { 322 getHibernateSession().lock(object, lockMode); 323 } 324 325 public void lock(String entityName, Object object, LockMode lockMode) 326 throws HibernateException 327 { 328 getHibernateSession().lock(entityName, object, lockMode); 329 } 330 331 public void refresh(Object object) 332 throws HibernateException 333 { 334 getHibernateSession().refresh(object); 335 } 336 337 public void refresh(Object object, LockMode lockMode) 338 throws HibernateException 339 { 340 getHibernateSession().refresh(object, lockMode); 341 } 342 343 public LockMode getCurrentLockMode(Object object) 344 throws HibernateException 345 { 346 return getHibernateSession().getCurrentLockMode(object); 347 } 348 349 public Transaction beginTransaction() 350 throws HibernateException 351 { 352 return getHibernateSession().beginTransaction(); 353 } 354 355 public Criteria createCriteria(Class persistentClass) 356 { 357 return getHibernateSession().createCriteria(persistentClass); 358 } 359 360 public Criteria createCriteria(Class persistentClass, String alias) 361 { 362 return getHibernateSession().createCriteria(persistentClass, alias); 363 } 364 365 public Criteria createCriteria(String entityName) 366 { 367 return getHibernateSession().createCriteria(entityName); 368 } 369 370 public Criteria createCriteria(String entityName, String alias) 371 { 372 return getHibernateSession().createCriteria(entityName, alias); 373 } 374 375 public org.hibernate.Query createQuery(String queryString) 376 throws HibernateException 377 { 378 return getHibernateSession().createQuery(queryString); 379 } 380 381 public SQLQuery createSQLQuery(String queryString) 382 throws HibernateException 383 { 384 return getHibernateSession().createSQLQuery(queryString); 385 } 386 387 public org.hibernate.Query createFilter(Object collection, String queryString) 388 throws HibernateException 389 { 390 return getHibernateSession().createFilter(collection, queryString); 391 } 392 393 public org.hibernate.Query getNamedQuery(String queryName) 394 throws HibernateException 395 { 396 return getHibernateSession().getNamedQuery(queryName); 397 } 398 399 public void clear() 400 { 401 getHibernateSession().clear(); 402 } 403 404 public Object get(Class clazz, Serializable id) 405 throws HibernateException 406 { 407 return getHibernateSession().get(clazz, id); 408 } 409 410 public Object get(Class clazz, Serializable id, LockMode lockMode) 411 throws HibernateException 412 { 413 return getHibernateSession().get(clazz, id, lockMode); 414 } 415 416 public Object get(String entityName, Serializable id) 417 throws HibernateException 418 { 419 return getHibernateSession().get(entityName, id); 420 } 421 422 public Object get(String entityName, Serializable id, LockMode lockMode) 423 throws HibernateException 424 { 425 return getHibernateSession().get(entityName, id, lockMode); 426 } 427 428 public String getEntityName(Object object) 429 throws HibernateException 430 { 431 return getHibernateSession().getEntityName(object); 432 } 433 434 public Filter enableFilter(String filterName) 435 { 436 return getHibernateSession().enableFilter(filterName); 437 } 438 439 public Filter getEnabledFilter(String filterName) 440 { 441 return getHibernateSession().getEnabledFilter(filterName); 442 } 443 444 public void disableFilter(String filterName) 445 { 446 getHibernateSession().disableFilter(filterName); 447 } 448 449 public SessionStatistics getStatistics() 450 { 451 return getHibernateSession().getStatistics(); 452 } 453 454 public void setReadOnly(Object entity, boolean readOnly) 455 { 456 getHibernateSession().setReadOnly(entity, readOnly); 457 } 458 459 public Transaction getTransaction() 460 { 461 return getHibernateSession().getTransaction(); 462 } 463 } 464 | Popular Tags |