1 19 20 package org.apache.cayenne.jpa; 21 22 import javax.persistence.EntityManager; 23 import javax.persistence.EntityNotFoundException; 24 import javax.persistence.EntityTransaction; 25 import javax.persistence.FlushModeType; 26 import javax.persistence.LockModeType; 27 import javax.persistence.PersistenceException; 28 import javax.persistence.Query; 29 30 import org.apache.cayenne.CayenneRuntimeException; 31 import org.apache.cayenne.DataChannel; 32 import org.apache.cayenne.DataObjectUtils; 33 import org.apache.cayenne.ObjectContext; 34 import org.apache.cayenne.PersistenceState; 35 import org.apache.cayenne.Persistent; 36 import org.apache.cayenne.access.Transaction; 37 import org.apache.cayenne.query.EJBQLQuery; 38 39 public class ResourceLocalEntityManager implements EntityManager, CayenneEntityManager { 40 41 protected EntityTransaction transaction; 42 protected ResourceLocalEntityManagerFactory factory; 43 protected FlushModeType flushMode; 44 protected boolean open; 45 protected ObjectContext context; 46 47 public ResourceLocalEntityManager(ObjectContext context, 48 ResourceLocalEntityManagerFactory factory) { 49 50 if (factory == null) { 51 throw new IllegalArgumentException ("Null entity manager factory"); 52 } 53 54 this.open = true; 55 this.context = context; 56 this.factory = factory; 57 } 58 59 62 public DataChannel getChannel() { 63 return context.getChannel(); 64 } 65 66 69 protected ResourceLocalEntityManagerFactory getFactory() { 70 return factory; 71 } 72 73 82 public void close() { 83 checkClosed(); 84 85 if (transaction != null && transaction.isActive()) { 86 throw new IllegalStateException ("Active transaction in progress"); 87 } 88 89 open = false; 90 } 91 92 public boolean isOpen() { 93 return open && (factory == null || factory.isOpen()); 94 } 95 96 public Object getDelegate() { 97 return factory.getProvider(); 98 } 99 100 106 public void persist(Object entity) { 107 checkClosed(); 108 context.registerNewObject(entity); 109 } 110 111 120 public <T> T merge(T entity) { 121 checkClosed(); 122 checkNotRemoved(entity); 123 Persistent persistent = (Persistent) entity; 124 return (T) context.localObject(persistent.getObjectId(), persistent); 125 } 126 127 133 public void remove(Object entity) { 134 checkClosed(); 135 checkAttached(entity); 136 context.deleteObject((Persistent) entity); 137 } 138 139 148 public <T> T find(Class <T> entityClass, Object primaryKey) { 149 checkClosed(); 150 return (T) DataObjectUtils.objectForPK(context, entityClass, primaryKey); 151 } 152 153 169 public <T> T getReference(Class <T> entityClass, Object primaryKey) { 170 checkClosed(); 171 172 T ref = find(entityClass, primaryKey); 174 175 if (ref == null) { 176 throw new EntityNotFoundException("Could not find " 177 + entityClass.toString() 178 + " with primary key value " 179 + primaryKey.toString()); 180 } 181 182 return ref; 183 } 184 185 190 public void flush() { 191 checkClosed(); 192 193 try { 194 context.commitChanges(); 195 } 196 catch (CayenneRuntimeException e) { 197 throw new PersistenceException(e); 198 } 199 } 200 201 207 public void setFlushMode(FlushModeType flushMode) { 208 checkClosed(); 209 210 this.flushMode = flushMode; 211 } 212 213 219 public FlushModeType getFlushMode() { 220 checkClosed(); 221 222 return flushMode; 223 } 224 225 233 public void refresh(Object entity) { 234 checkClosed(); 235 throw new UnsupportedOperationException ("TODO"); 237 } 238 239 244 public void clear() { 245 checkClosed(); 246 throw new UnsupportedOperationException ("TODO"); 248 } 249 250 255 public boolean contains(Object entity) { 256 checkClosed(); 257 Persistent p = (Persistent) entity; 258 return context.getGraphManager().getNode(p.getObjectId()) == p; 259 } 260 261 268 public Query createQuery(String ejbqlString) { 269 checkClosed(); 270 271 JpaQuery query = new JpaQuery(context); 272 query.setQuery(new EJBQLQuery(ejbqlString)); 273 return query; 274 } 275 276 284 public Query createNamedQuery(String name) { 285 checkClosed(); 286 return new JpaQuery(context, name); 287 } 288 289 public Query createNativeQuery(String sqlString, Class resultClass) { 290 checkClosed(); 291 return new JpaNativeQuery(context, sqlString, resultClass); 292 } 293 294 301 public Query createNativeQuery(String sqlString) { 302 checkClosed(); 303 304 return new JpaNativeQuery(context, sqlString, factory 305 .getPersistenceUnitInfo() 306 .getPersistenceUnitName()); 307 } 308 309 316 public Query createNativeQuery(String sqlString, String resultSetMapping) { 317 checkClosed(); 318 319 throw new UnsupportedOperationException ("TODO"); 321 } 322 323 334 public void joinTransaction() { 335 throw new JpaProviderException( 336 "'joinTransaction' is called on a RESOURCE_LOCAL EntityManager"); 337 } 338 339 public void lock(Object entity, LockModeType lockMode) { 340 } 342 343 349 public EntityTransaction getTransaction() { if (transaction == null) { 351 this.transaction = new JpaTransaction( 352 Transaction.internalTransaction(null), 353 this); 354 } 355 356 return transaction; 357 } 358 359 363 protected void checkAttached(Object entity) throws IllegalArgumentException { 364 Persistent p = (Persistent) entity; 365 if (p.getPersistenceState() == PersistenceState.TRANSIENT 366 || p.getObjectContext() == null) { 367 throw new IllegalArgumentException ("entity is detached: " + entity); 368 } 369 } 370 371 375 protected void checkNotRemoved(Object entity) throws IllegalArgumentException { 376 Persistent p = (Persistent) entity; 377 if (p.getPersistenceState() == PersistenceState.DELETED) { 378 throw new IllegalArgumentException ("entity is removed: " + entity); 379 } 380 } 381 382 385 protected void checkClosed() throws IllegalStateException { 386 if (!isOpen()) { 387 throw new IllegalStateException ( 388 "An attempt to access closed EntityManagerFactory."); 389 } 390 } 391 } 392 | Popular Tags |