1 7 package org.jboss.ejb3.entity; 8 9 import java.io.Serializable ; 10 import javax.naming.InitialContext ; 11 import javax.naming.NamingException ; 12 import javax.persistence.EntityManager; 13 import javax.persistence.EntityTransaction; 14 import javax.persistence.FlushModeType; 15 import javax.persistence.Query; 16 import org.hibernate.Session; 17 import org.hibernate.ejb.HibernateEntityManager; 18 19 20 24 public class InjectedEntityManager implements EntityManager, HibernateSession, Serializable 25 { 26 private transient ManagedEntityManagerFactory factory; 27 private String jndiName; 28 29 public Session getHibernateSession() 30 { 31 if (getSession() instanceof HibernateEntityManager) 32 { 33 return ((HibernateEntityManager)getSession()).getSession(); 34 } 35 throw new RuntimeException ("ILLEGAL ACTION: Not a Hibernate persistence provider"); 36 } 37 38 public InjectedEntityManager(ManagedEntityManagerFactory factory) 39 { 40 if (factory == null) throw new NullPointerException ("factory must not be null"); 41 this.factory = factory; 42 this.jndiName = factory.getJndiName(); 43 } 44 45 public InjectedEntityManager() 46 { 47 48 } 49 50 public <T> T getReference(Class <T> entityClass, Object primaryKey) 51 { 52 return getSession().getReference(entityClass, primaryKey); 53 } 54 55 public void setFlushMode(FlushModeType flushMode) 56 { 57 getSession().setFlushMode(flushMode); 58 } 59 60 public Query createQuery(String ejbqlString) 61 { 62 return getSession().createQuery(ejbqlString); 63 } 64 65 public Query createNamedQuery(String name) 66 { 67 return getSession().createNamedQuery(name); 68 } 69 70 public Query createNativeQuery(String sqlString) 71 { 72 return getSession().createNativeQuery(sqlString); 73 } 74 75 public Query createNativeQuery(String sqlString, Class resultClass) 76 { 77 return getSession().createNativeQuery(sqlString, resultClass); 78 } 79 80 public Query createNativeQuery(String sqlString, String resultSetMapping) 81 { 82 return getSession().createNativeQuery(sqlString, resultSetMapping); 83 } 84 85 public <A> A find(Class <A> entityClass, Object primaryKey) 86 { 87 return getSession().find(entityClass, primaryKey); 88 } 89 90 public void persist(Object entity) 91 { 92 getSession().persist(entity); 93 } 94 95 public <A> A merge(A entity) 96 { 97 return (A) getSession().merge(entity); 98 } 99 100 public void remove(Object entity) 101 { 102 getSession().remove(entity); 103 } 104 105 public void refresh(Object entity) 106 { 107 getSession().refresh(entity); 108 } 109 110 public boolean contains(Object entity) 111 { 112 return getSession().contains(entity); 113 } 114 115 public void flush() 116 { 117 getSession().flush(); 118 } 119 120 public void close() 121 { 122 throw new IllegalStateException ("Illegal to call this method from injected, managed EntityManager"); 123 } 124 125 public boolean isOpen() 126 { 127 throw new IllegalStateException ("Illegal to call this method from injected, managed EntityManager"); 128 } 129 130 public EntityTransaction getTransaction() 131 { 132 throw new IllegalStateException ("Illegal to call this method from injected, managed EntityManager"); 133 } 134 135 protected EntityManager getSession() 136 { 137 if (factory == null) 138 { 139 try 140 { 141 InitialContext ctx = new InitialContext (); 142 factory = (ManagedEntityManagerFactory)ctx.lookup(jndiName); 143 } 144 catch (NamingException e) 145 { 146 throw new RuntimeException (e); 147 } 148 } 149 return factory.getSession(); 150 } 151 152 } 153 | Popular Tags |