1 package org.hibernate.ejb; 3 4 import javax.persistence.PersistenceContextType; 5 6 import org.hibernate.Session; 7 import org.hibernate.SessionFactory; 8 import org.hibernate.engine.SessionImplementor; 9 10 13 public class EntityManagerImpl extends AbstractEntityManagerImpl { 14 15 protected Session session; 16 protected SessionFactory sessionFactory; 17 protected boolean open; 18 19 public EntityManagerImpl(SessionFactory sessionFactory, PersistenceContextType type) { 20 super( type ); 21 this.sessionFactory = sessionFactory; 22 open = true; 23 } 24 25 public Session getSession() { 26 27 if ( !open ) throw new IllegalStateException ( "EntityManager is closed" ); 28 29 if ( session == null ) { 30 session = sessionFactory.openSession(); 31 if ( type == PersistenceContextType.TRANSACTION ) { 32 ( (SessionImplementor) session ).setAutoClear( true ); 33 } 34 } 35 36 return session; 37 38 } 39 40 public void close() { 41 42 if ( !open ) throw new IllegalStateException ( "EntityManager is closed" ); 43 44 open = false; 45 if ( session != null ) session.close(); 46 47 } 48 49 public boolean isOpen() { 50 return open; 51 } 52 53 } 54 | Popular Tags |