amberconnection.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,682 行 · 第 1/5 页
JAVA
2,682 行
Class resultClass = nativeQuery.getResultClass(); AmberEntityHome entityHome = _persistenceUnit.getEntityHome(resultClass.getName()); EntityType entityType = entityHome.getEntityType(); try { return createNativeQuery(sql, entityType.getInstanceClass()); } catch (Exception e) { throw new IllegalArgumentException(e); } } /** * Creates an instance of the named query */ public Query createNativeQuery(String sql) { sql = sql.trim(); char ch = sql.charAt(0); if (ch == 'S' || ch == 's') throw new UnsupportedOperationException(L.l("createNativeQuery(String sql) is not supported for select statements. Please use createNativeQuery(String sql, String map) or createNativeQuery(String sql, Class cl) to map the result to scalar values or bean classes.")); return createInternalNativeQuery(sql); } /** * Creates an instance of the named query */ public Query createNativeQuery(String sql, String map) { // jpa/0y1- SqlResultSetMappingConfig resultSet; resultSet = _persistenceUnit.getSqlResultSetMapping(map); if (resultSet == null) throw new IllegalArgumentException(L.l("createNativeQuery() cannot create a native query for a result set named '{0}'", map)); return createInternalNativeQuery(sql, resultSet); } /** * Creates an instance of the native query */ public Query createNativeQuery(String sql, Class type) { SqlResultSetMappingConfig resultSet = new SqlResultSetMappingConfig(); EntityResultConfig entityResult = new EntityResultConfig(); entityResult.setEntityClass(type.getName()); resultSet.addEntityResult(entityResult); return createInternalNativeQuery(sql, resultSet); } /** * Refresh the state of the instance from the database. */ public void refresh(Object entity) { try { if (entity == null) return; if (! (entity instanceof Entity)) throw new IllegalArgumentException(L.l("refresh() operation can only be applied to an entity instance. This object is of class '{0}'", entity.getClass().getName())); checkTransactionRequired("refresh"); Entity instance = (Entity) entity; String className = instance.getClass().getName(); Object pk = instance.__caucho_getPrimaryKey(); Entity oldEntity = getEntity(instance.getClass(), pk); if (oldEntity != null) { EntityState state = instance.__caucho_getEntityState(); if (state.ordinal() <= EntityState.TRANSIENT.ordinal() || EntityState.P_DELETING.ordinal() <= state.ordinal()) { throw new IllegalArgumentException(L.l("refresh() operation can only be applied to a managed entity instance. The entity state is '{0}' for object of class '{0}' with PK '{1}'", className, pk, state == EntityState.TRANSIENT ? "TRANSIENT" : "DELETING or DELETED")); } } else throw new IllegalArgumentException(L.l("refresh() operation can only be applied to a managed entity instance. There was no managed instance of class '{0}' with PK '{1}'", className, pk)); // Reset and refresh state. instance.__caucho_expire(); instance.__caucho_makePersistent(this, (EntityType) null); instance.__caucho_retrieve_eager(this); } catch (SQLException e) { throw new AmberRuntimeException(e); } } /** * Returns the flush mode. */ public FlushModeType getFlushMode() { return FlushModeType.AUTO; } /** * Sets the extended type. */ public void setExtended(boolean isExtended) { _isExtended = isExtended; } /** * Returns the flush mode. */ public void setFlushMode(FlushModeType mode) { throw new UnsupportedOperationException(); } /** * Locks the object. */ public void lock(Object entity, LockModeType lockMode) { throw new UnsupportedOperationException(); } /** * Returns the transaction. */ public EntityTransaction getTransaction() { if (_isXA) throw new IllegalStateException(L.l("Cannot call EntityManager.getTransaction() inside a distributed transaction.")); if (_trans == null) _trans = new EntityTransactionImpl(); return _trans; } /** * Returns true if open. */ public boolean isOpen() { return _persistenceUnit != null; } /** * Registers with the local transaction. */ void register() { if (! _isRegistered) { if (! _isAppManaged) UserTransactionProxy.getInstance().enlistCloseResource(this); UserTransactionProxy.getInstance().enlistBeginResource(this); } _isRegistered = true; } /** * Joins the transaction. */ public void joinTransaction() { // XXX: jpa/0s46, jpa/0s47 _isInTransaction = true; } /** * Gets the delegate. */ public Object getDelegate() { throw new UnsupportedOperationException(); } /** * Closes the context. */ public void close() { if (_persistenceUnit == null) { // jpa/0s45 throw new IllegalStateException("Entity manager is already closed."); } try { if (_isThreadConnection) _persistenceUnit.removeThreadConnection(); _isRegistered = false; cleanup(); } catch (Exception e) { log.log(Level.FINER, e.toString(), e); } finally { _persistenceUnit = null; } } /** * Returns the amber manager. */ public AmberPersistenceUnit getAmberManager() { return _persistenceUnit; } /** * Registers a collection. */ public void register(AmberCollection query) { _queries.add(query); } /** * Adds a completion */ public void addCompletion(AmberCompletion completion) { if (! _completionList.contains(completion)) _completionList.add(completion); } /** * Returns true if a transaction is active or * this persistence context is extended. */ public boolean isActiveTransaction() { return _isInTransaction || _isExtended; } /** * Returns true if a transaction is active. */ public boolean isInTransaction() { return _isInTransaction; } /** * Returns the cache chunk size. */ public int getCacheChunkSize() { return 25; } public Object load(Class cl, Object key, boolean isEager) throws AmberException { if (_persistenceUnit == null) throw new IllegalStateException(L.l("AmberConnection is closed")); if (log.isLoggable(Level.FINER)) log.finer(L.l("{0}[1] amber loading entity class", cl.getSimpleName(), key)); Entity entity = null; if (key == null) return null; // ejb/0d01, jpa/0gh0, jpa/0g0k, jpa/0j5f // if (shouldRetrieveFromCache()) entity = getEntity(cl, key); if (entity != null) { // jpa/0s2d: if it contains such entity and we have // PersistenceContextType.TRANSACTION, the entity is // managed and we can just return it (otherwise it would // be detached and not be found in _entities). // XXX: for PersistenceContextType.EXTENDED??? return entity; } _entityKey.init(cl, key); EntityItem cacheItem = loadCacheItem(cl, key, null); if (cacheItem == null) return null; /* boolean isLoad = true; // jpa/0h13 as a negative test. if (isActiveTransaction()) isLoad = isEager; */ // jpa/0o03 boolean isLoad = isEager; try { entity = cacheItem.createEntity(this, key); if (entity == null) return null; // The entity is added for eager loading addInternalEntity(entity); boolean isXA = isActiveTransaction(); // jpa/0l48: inheritance loading optimization. // jpa/0h20: no transaction, copy from the existing cache item. // jpa/0l42: loading optimization. if (isLoad) { entity.__caucho_retrieve_eager(this); } else if (isXA) { // jpa/0v33: within a transaction, cannot copy from cache. entity.__caucho_retrieve_self(this); } } catch (SQLException e) { if (_persistenceUnit.isJPA()) { log.log(Level.FINER, e.toString(), e); return null; } throw new AmberObjectNotFoundException(L.l("{0}[{1}] is an unknown amber object", cl.getName(), key), e); } catch (AmberObjectNotFoundException e) { // 0g0q: if the entity is not found, removes it from context. if (entity != null) removeEntity(entity); if (_persistenceUnit.isJPA()) return null; throw e; } Entity txEntity = getTransactionEntity(entity.getClass(), entity.__caucho_getPrimaryKey()); // XXX: jpa/0v33 if (txEntity != null) setTransactionalState(txEntity); return entity; } public EntityItem loadCacheItem(Class cl, Object key, AmberEntityHome entityHome) throws AmberException { _entityKey.init(cl, key); EntityItem cacheItem = _persistenceUnit.getEntity(_entityKey); if (cacheItem != null) return cacheItem; if (entityHome == null) entityHome = _persistenceUnit.getEntityHome(cl.getName()); if (entityHome == null) { throw new IllegalArgumentException(L.l("'{0}' is an unknown class in persistence-unit '{1}'. find() operation can only be applied if the entity class is specified in the scope of a persistence unit.", cl.getName(), _persistenceUnit.getName())); } cacheItem = entityHome.findEntityItem(this, key); if (cacheItem == null) { if (_persistenceUnit.isJPA()) return null; // ejb/0604 throw new AmberObjectNotFoundException("amber find: no matching object " + cl.getName() + "[" + key + "]"); } if (cacheItem instanceof CacheableEntityItem) cacheItem = _persistenceUnit.putEntity(cl, key, cacheItem); return cacheItem; } /** * Loads the object based on the class and primary key. */ public Object load(String entityName, Object key) throws AmberException { AmberEntityHome entityHome = _persistenceUnit.getEntityHome(entityName); if (entityHome == null) return null; Entity entity = null; // XXX: ejb/0d01 // jpa/0y14 if (shouldRetrieveFromCache()) entity = getEntity(entityHome.getJavaClass(), key); if (entity != null) return entity; try { entityHome.init(); } catch (ConfigException e) { throw new AmberException(e); } entity = (Entity) find(entityHome.getEntityType().getInstanceClass(), key); // addEntity(entity); return entity; } /** * Returns the entity for the connection. */ public Entity getEntity(EntityItem item) { Entity itemEntity = item.getEntity(); Class cl = itemEntity.getClass(); Object pk = itemEntity.__caucho_getPrimaryKey(); Entity entity = getEntity(cl, pk); if (entity != null) { if (entity.__caucho_getEntityState().isManaged()) return entity; // else // jpa/0g40: the copy object was created at some point in // findEntityItem, but it is still not loaded. } else { try { entity = item.createEntity(this, pk); } catch (SQLException e) { throw new AmberRuntimeException(e); } /* // Create a new entity for the given class and primary key. try { entity = (Entity) cl.newInstance(); } catch (Exception e) { throw new AmberRuntimeException(e); } */ // entity.__caucho_setEntityState(EntityState.P_NON_TRANSACTIONAL); // entity.__caucho_setPrimaryKey(pk); // jpa/1000: avoids extra allocations. addInternalEntity(entity); } // jpa/0l43 //_persistenceUnit.copyFromCacheItem(this, entity, item); // jpa/0l4a entity.__caucho_retrieve_eager(this); return entity; } /** * Returns the entity for the connection. */ public Entity getEntityLazy(EntityItem item) { Entity itemEntity = item.getEntity(); Class cl = itemEntity.getClass(); Object pk = itemEntity.__caucho_getPrimaryKey(); Entity entity = getEntity(cl, pk); if (entity != null) { if (entity.__caucho_getEntityState().isManaged()) return entity; // else // jpa/0g40: the copy object was created at some point in // findEntityItem, but it is still not loaded. } else { try { entity = item.createEntity(this, pk); } catch (SQLException e) { throw new AmberRuntimeException(e); } /* // Create a new entity for the given class and primary key. try { entity = (Entity) cl.newInstance(); } catch (Exception e) { throw new AmberRuntimeException(e); } */ // entity.__caucho_setEntityState(EntityState.P_NON_TRANSACTIONAL); // entity.__caucho_setPrimaryKey(pk); // jpa/1000: avoids extra allocations. addInternalEntity(entity); } return entity; } /** * Loads the object based on itself. */ public Object makePersistent(Object obj) throws SQLException {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?