1 22 package org.jboss.ejb3.entity; 23 24 import org.hibernate.Session; 25 import org.hibernate.ejb.HibernateEntityManager; 26 import org.jboss.ejb3.PersistenceUnitRegistry; 27 28 import javax.persistence.*; 29 import java.io.Externalizable ; 30 import java.io.IOException ; 31 import java.io.ObjectInput ; 32 import java.io.ObjectOutput ; 33 34 35 39 public class TransactionScopedEntityManager implements EntityManager, HibernateSession, Externalizable 40 { 41 private transient ManagedEntityManagerFactory factory; 42 43 public Session getHibernateSession() 44 { 45 EntityManager em = factory.getTransactionScopedEntityManager(); 46 if (em instanceof HibernateEntityManager) 47 { 48 return ((HibernateEntityManager) em).getSession(); 49 } 50 throw new RuntimeException ("ILLEGAL ACTION: Not a Hibernate pe" + 51 "rsistence provider"); 52 } 53 54 public TransactionScopedEntityManager(ManagedEntityManagerFactory factory) 55 { 56 if (factory == null) throw new NullPointerException ("factory must not be null"); 57 this.factory = factory; 58 } 59 60 public TransactionScopedEntityManager() 61 { 62 } 63 64 public void writeExternal(ObjectOutput out) throws IOException 65 { 66 out.writeUTF(factory.getKernelName()); 67 } 68 69 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 70 { 71 String kernelName = in.readUTF(); 72 PersistenceUnitDeployment deployment = PersistenceUnitRegistry.getPersistenceUnit(kernelName); 73 if (deployment == null) throw new IOException ("Unable to find persistence unit in registry: " + kernelName); 74 factory = deployment.getManagedFactory(); 75 } 76 77 public Object getDelegate() 78 { 79 return factory.getTransactionScopedEntityManager(); 80 } 81 82 public void joinTransaction() 83 { 84 factory.verifyInTx(); 85 factory.getTransactionScopedEntityManager().joinTransaction(); 86 } 87 88 public void clear() 89 { 90 factory.getTransactionScopedEntityManager().clear(); 91 } 92 93 public FlushModeType getFlushMode() 94 { 95 return factory.getTransactionScopedEntityManager().getFlushMode(); 96 } 97 98 public void lock(Object entity, LockModeType lockMode) 99 { 100 factory.verifyInTx(); 101 factory.getTransactionScopedEntityManager().lock(entity, lockMode); 102 } 103 104 public <T> T getReference(Class <T> entityClass, Object primaryKey) 105 { 106 EntityManager em = factory.getTransactionScopedEntityManager(); 107 if (!factory.isInTx()) em.clear(); try 109 { 110 return em.getReference(entityClass, primaryKey); 111 } 112 finally 113 { 114 if (!factory.isInTx()) em.clear(); } 116 } 117 118 public void setFlushMode(FlushModeType flushMode) 119 { 120 factory.getTransactionScopedEntityManager().setFlushMode(flushMode); 121 } 122 123 public Query createQuery(String ejbqlString) 124 { 125 EntityManager em = factory.getTransactionScopedEntityManager(); 126 if (!factory.isInTx()) em.clear(); return em.createQuery(ejbqlString); 128 } 129 130 public Query createNamedQuery(String name) 131 { 132 EntityManager em = factory.getTransactionScopedEntityManager(); 133 if (!factory.isInTx()) em.clear(); return em.createNamedQuery(name); 135 } 136 137 public Query createNativeQuery(String sqlString) 138 { 139 EntityManager em = factory.getTransactionScopedEntityManager(); 140 if (!factory.isInTx()) em.clear(); return em.createNativeQuery(sqlString); 142 } 143 144 public Query createNativeQuery(String sqlString, Class resultClass) 145 { 146 EntityManager em = factory.getTransactionScopedEntityManager(); 147 if (!factory.isInTx()) em.clear(); return em.createNativeQuery(sqlString, resultClass); 149 } 150 151 public Query createNativeQuery(String sqlString, String resultSetMapping) 152 { 153 EntityManager em = factory.getTransactionScopedEntityManager(); 154 if (!factory.isInTx()) em.clear(); return em.createNativeQuery(sqlString, resultSetMapping); 156 } 157 158 public <A> A find(Class <A> entityClass, Object primaryKey) 159 { 160 EntityManager em = factory.getTransactionScopedEntityManager(); 161 if (!factory.isInTx()) em.clear(); try 163 { 164 return em.find(entityClass, primaryKey); 165 } 166 finally 167 { 168 if (!factory.isInTx()) em.clear(); } 170 } 171 172 public void persist(Object entity) 173 { 174 factory.verifyInTx(); 175 factory.getTransactionScopedEntityManager().persist(entity); 176 } 177 178 public <A> A merge(A entity) 179 { 180 factory.verifyInTx(); 181 return (A) factory.getTransactionScopedEntityManager().merge(entity); 182 } 183 184 public void remove(Object entity) 185 { 186 factory.verifyInTx(); 187 factory.getTransactionScopedEntityManager().remove(entity); 188 } 189 190 public void refresh(Object entity) 191 { 192 factory.verifyInTx(); 193 factory.getTransactionScopedEntityManager().refresh(entity); 194 } 195 196 public boolean contains(Object entity) 197 { 198 return factory.getTransactionScopedEntityManager().contains(entity); 199 } 200 201 public void flush() 202 { 203 factory.verifyInTx(); 204 factory.getTransactionScopedEntityManager().flush(); 205 } 206 207 public void close() 208 { 209 throw new IllegalStateException ("Illegal to call this method from injected, managed EntityManager"); 210 } 211 212 public boolean isOpen() 213 { 214 throw new IllegalStateException ("Illegal to call this method from injected, managed EntityManager"); 215 } 216 217 public EntityTransaction getTransaction() 218 { 219 throw new IllegalStateException ("Illegal to call this method from injected, managed EntityManager"); 220 } 221 222 } 223 | Popular Tags |