1 17 18 package org.apache.geronimo.persistence; 19 20 import java.util.Map ; 21 22 import javax.persistence.EntityManager; 23 import javax.persistence.EntityManagerFactory; 24 import javax.persistence.EntityTransaction; 25 import javax.persistence.FlushModeType; 26 import javax.persistence.LockModeType; 27 import javax.persistence.Query; 28 29 import org.apache.geronimo.transaction.manager.TransactionManagerImpl; 30 31 34 public class CMPEntityManagerExtended implements EntityManager { 35 36 private final TransactionManagerImpl transactionManager; 37 private final String persistenceUnit; 38 private final EntityManagerFactory entityManagerFactory; 39 private final Map entityManagerProperties; 40 private final InternalCMPEntityManagerExtended entityManager; 41 42 public CMPEntityManagerExtended(TransactionManagerImpl transactionManager, String persistenceUnit, EntityManagerFactory entityManagerFactory, Map entityManagerProperties) { 43 this.transactionManager = transactionManager; 44 this.persistenceUnit = persistenceUnit; 45 this.entityManagerFactory = entityManagerFactory; 46 this.entityManagerProperties = entityManagerProperties; 47 entityManager = getEntityManager(); 48 } 49 50 private InternalCMPEntityManagerExtended getEntityManager() { 51 InternalCMPEntityManagerExtended entityManager = EntityManagerExtendedRegistry.getEntityManager(persistenceUnit); 52 if (entityManager == null) { 53 entityManager = createEntityManager(); 54 EntityManagerExtendedRegistry.putEntityManager(persistenceUnit, entityManager); 55 } 56 entityManager.registerBean(); 57 return entityManager; 58 } 59 60 private InternalCMPEntityManagerExtended createEntityManager() { 61 EntityManager entityManager; 62 if (entityManagerProperties == null) { 63 entityManager = entityManagerFactory.createEntityManager(); 64 } else { 65 entityManager = entityManagerFactory.createEntityManager(entityManagerProperties); 66 } 67 return new InternalCMPEntityManagerExtended(entityManager, persistenceUnit, transactionManager); 68 } 69 70 public void beanRemoved() { 71 entityManager.beanRemoved(); 72 } 73 74 75 public void persist(Object o) { 76 entityManager.persist(o); 77 } 78 79 public <T>T merge(T t) { 80 return entityManager.merge(t); 81 } 82 83 public void remove(Object o) { 84 entityManager.remove(o); 85 } 86 87 public <T>T find(Class <T> aClass, Object o) { 88 return entityManager.find(aClass, o); 89 } 90 91 public <T>T getReference(Class <T> aClass, Object o) { 92 return entityManager.getReference(aClass, o); 93 } 94 95 public void flush() { 96 entityManager.flush(); 97 } 98 99 public void setFlushMode(FlushModeType flushModeType) { 100 entityManager.setFlushMode(flushModeType); 101 } 102 103 public FlushModeType getFlushMode() { 104 return entityManager.getFlushMode(); 105 } 106 107 public void lock(Object o, LockModeType lockModeType) { 108 entityManager.lock(o, lockModeType); 109 } 110 111 public void refresh(Object o) { 112 entityManager.refresh(o); 113 } 114 115 public void clear() { 116 entityManager.clear(); 117 } 118 119 public boolean contains(Object o) { 120 return entityManager.contains(o); 121 } 122 123 public Query createQuery(String s) { 124 return entityManager.createQuery(s); 125 } 126 127 public Query createNamedQuery(String s) { 128 return entityManager.createNamedQuery(s); 129 } 130 131 public Query createNativeQuery(String s) { 132 return entityManager.createNativeQuery(s); 133 } 134 135 public Query createNativeQuery(String s, Class aClass) { 136 return entityManager.createNativeQuery(s, aClass); 137 } 138 139 public Query createNativeQuery(String s, String s1) { 140 return entityManager.createNativeQuery(s, s1); 141 } 142 143 public void close() { 144 throw new IllegalStateException ("You cannot call close on a Container Managed Entity Manager"); 145 } 146 147 public boolean isOpen() { 148 return true; 149 } 150 151 public EntityTransaction getTransaction() { 152 throw new IllegalStateException ("You cannot call getTransaction on a container managed EntityManager"); 153 } 154 155 public void joinTransaction() { 156 throw new IllegalStateException ("You cannot call joinTransaction on a container managed EntityManager"); 157 } 158 159 public Object getDelegate() { 160 return entityManager.getDelegate(); 161 } 162 163 } 164 | Popular Tags |