amberconnection.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,682 行 · 第 1/5 页

JAVA
2,682
字号
    Entity entity = (Entity) obj;    // check to see if exists    if (entity == null)      throw new NullPointerException();    Class cl = entity.getClass();    // Entity oldEntity = getEntity(cl, entity.__caucho_getPrimaryKey());    AmberEntityHome entityHome;    entityHome = _persistenceUnit.getEntityHome(entity.getClass().getName());    if (entityHome == null)      throw new AmberException(L.l("{0}: entity has no matching home",				   entity.getClass().getName()));    entityHome.makePersistent(entity, this, false);    return entity;  }  /**   * Loads the object with the given class.   */  public Entity loadLazy(Class cl, String name, Object key)  {    return loadLazy(cl.getName(), name, key);  }  /**   * Loads the object with the given class.   */  public Entity loadLazy(String className, String name, Object key)  {    if (key == null)      return null;    try {      AmberEntityHome home = _persistenceUnit.getEntityHome(name);      if (home == null)        throw new RuntimeException(L.l("no matching home for {0}", className));      home.init();      Object obj = load(home.getEntityType().getInstanceClass(), key, false);      Entity entity = (Entity) obj;      return entity;    } catch (SQLException e) {      log.log(Level.WARNING, e.toString(), e);      return null;    } catch (ConfigException e) {      throw new AmberRuntimeException(e);    }  }  /**   * Loads the object with the given class.   */  public EntityItem findEntityItem(String name, Object key)  {    try {      AmberEntityHome home = _persistenceUnit.getEntityHome(name);      if (home == null)        throw new RuntimeException(L.l("no matching home for {0}", name));      home.init();      return loadCacheItem(home.getJavaClass(), key, home);    } catch (RuntimeException e) {      throw e;    } catch (Exception e) {      throw new AmberRuntimeException(e);    }  }  /**   * Loads the object with the given class.   */  public EntityItem setEntityItem(String name, Object key, EntityItem item)  {    try {      AmberEntityHome home = _persistenceUnit.getEntityHome(name);      if (home == null)        throw new RuntimeException(L.l("no matching home for {0}", name));      home.init();      return home.setEntityItem(key, item);    } catch (RuntimeException e) {      throw e;    } catch (Exception e) {      throw new AmberRuntimeException(e);    }  }  /**   * Loads the object with the given class.   *   * @param name the class name.   * @param key the key.   * @param notExpiringLoadMask the load mask bit that will not be reset   *        when the entity is expiring and reloaded to a new transaction.   *        Normally, the bit is only set in bidirectional one-to-one   *        relationships where we already know the other side has already   *        been loaded in the second or new transactions.   * @param notExpiringGroup the corresponding load group.   */  public Entity loadFromHome(String name,                             Object key)  {    try {      AmberEntityHome home = _persistenceUnit.getEntityHome(name);      if (home == null)        throw new RuntimeException(L.l("no matching home for {0}", name));      home.init();      // jpa/0ge4, jpa/0o04, jpa/0o0b, jpa/0o0c: bidirectional optimization.      return (Entity) load(home.getEntityType().getInstanceClass(),                           key, true);    } catch (AmberObjectNotFoundException e) {      if (_persistenceUnit.isJPA()) {        if (log.isLoggable(Level.FINER))          log.log(Level.FINER, e.toString(), e);        // jpa/0h29        return null;      }      throw e;    } catch (RuntimeException e) {      throw e;    } catch (Exception e) {      throw new AmberRuntimeException(e);    }  }  /**   * Loads the object with the given class.   */  public Object loadProxy(String name,                          Object key)  {    if (key == null)      return null;    AmberEntityHome home = _persistenceUnit.getEntityHome(name);    if (home == null)      throw new RuntimeException(L.l("no matching home for {0}", name));    return loadProxy(home.getEntityType(), key);  }  /**   * Loads the object with the given class.   */  public Object loadProxy(EntityType type,                          Object key)  {    if (key == null)      return null;    Entity entity = getEntity(type.getInstanceClass(), key);    if (entity != null) {      // jpa/0m30      return entity;    }    try {      AmberEntityHome home = type.getHome();      EntityItem item = home.findEntityItem(this, key);      if (item == null)        return null;      EntityFactory factory = home.getEntityFactory();      Object newEntity = factory.getEntity(this, item);      if (_persistenceUnit.isJPA()) {        // jpa/0h29: eager loading.        Entity instance = (Entity) newEntity;        setTransactionalState(instance);      }      return newEntity;    } catch (SQLException e) {      log.log(Level.WARNING, e.toString(), e);      return null;    }  }  /**   * Loads the CMP 2.1 object for the given entityItem   */  public Object loadProxy(EntityItem entityItem)  {    Entity itemEntity = entityItem.getEntity();    /*    Class cl = itemEntity.getClass();    Object pk = itemEntity.__caucho_getPrimaryKey();    Entity entity = getEntity(cl.getName(), pk);    if (entity != null) {      // jpa/0m30      return entity;    }    */        AmberEntityHome home = entityItem.getEntityHome();    EntityFactory factory = home.getEntityFactory();    Object newEntity = factory.getEntity(this, entityItem);    return newEntity;  }  /**   * Loads the object based on the class and primary key.   */  public Object load(Class cl, long intKey)    throws AmberException  {    AmberEntityHome entityHome = _persistenceUnit.getEntityHome(cl.getName());    if (entityHome == null)      return null;    Object key = entityHome.toObjectKey(intKey);    return load(cl, key, true);  }  /**   * Loads the object based on the class and primary key.   */  public Object loadLazy(Class cl, long intKey)    throws AmberException  {    AmberEntityHome entityHome = _persistenceUnit.getEntityHome(cl.getName());    if (entityHome == null)      return null;    Object key = entityHome.toObjectKey(intKey);    return loadLazy(cl, cl.getName(), key);  }  /**   * Matches the entity.   */  public Entity getEntity(Class cl, Object key)  {    Entity []entities = _entities;    for (int i = _entitiesTop - 1; i >= 0; i--) {      Entity entity = entities[i];      if (entity.__caucho_match(cl, key)) {        return entity;      }    }    return null;  }  public Entity getEntity(int index)  {    return _entities[index];  }  /**   * Returns the context entity that corresponds to the   * entity passed in. The entity passed in is normally a   * cache entity but might be the context entity itself   * when we want to make sure the reference is to an   * entity in the persistence context.   */  public Entity getEntity(Entity entity)  {    if (entity == null)      return null;    return getEntity(entity.getClass(),                     entity.__caucho_getPrimaryKey());  }  public Entity getSubEntity(Class cl, Object key)  {    Entity []entities = _entities;    // jpa/0l43    for (int i = _entitiesTop - 1; i >= 0; i--) {      Entity entity = entities[i];      if (entity.__caucho_getPrimaryKey().equals(key)) {        if (cl.isAssignableFrom(entity.getClass()))          return entity;      }    }    return null;  }  /**   * Gets the cache item referenced by __caucho_item   * from an entity of class/subclass cl.   */  public EntityItem getSubEntityCacheItem(Class cl, Object key)  {    Entity []entities = _entities;    // jpa/0l4a    for (int i = _entitiesTop - 1; i >= 0; i--) {      Entity entity = entities[i];      if (entity.__caucho_getPrimaryKey().equals(key)) {        if (cl.isAssignableFrom(entity.getClass()))          return entity.__caucho_getCacheItem();      }    }    return null;  }  public Entity getTransactionEntity(Class cl, Object key)  {    Entity []entities = _txEntities;    for (int i = _txEntitiesTop - 1; i >= 0; i--) {      Entity entity = entities[i];      if (entity.__caucho_match(cl, key)) {        return entity;      }    }    return null;  }  public Entity getTransactionEntity(int index)  {    return _txEntities[index];  }  /**   * Adds a new entity for the given class name and key.   * The new entity object is supposed to be used as a   * copy from cache. This avoids the cache entity to   * be added to the context.   *   * @return null - if the entity is already in the context.   *         otherwise, it returns the new entity added to   *         the context.   */  public Entity addNewEntity(Class cl, Object key)    throws InstantiationException, IllegalAccessException  {    // jpa/0l43    Entity entity = getSubEntity(cl, key);    // If the entity is already in the context, it returns null.    if (entity != null)      return null;    if (_persistenceUnit.isJPA()) {      // XXX: needs to create based on the discriminator with inheritance.      // Create a new entity for the given class and primary key.      entity = (Entity) cl.newInstance();      // jpa/0s2d      entity.__caucho_setEntityState(EntityState.P_NON_TRANSACTIONAL);    }    else {      // HelperBean__Amber -> HelperBean      String className = cl.getSuperclass().getName();      AmberEntityHome entityHome = _persistenceUnit.getEntityHome(className);      if (entityHome == null) {        if (log.isLoggable(Level.FINER))          log.log(Level.FINER, L.l("Amber.addNewEntity: home not found for entity (class: '{0}' PK: '{1}')",                                   className, key));        return null;      }      EntityFactory factory = entityHome.getEntityFactory();      // TestBean__EJB      Object value = factory.getEntity(key);      Method cauchoGetBeanMethod = entityHome.getCauchoGetBeanMethod();      if (cauchoGetBeanMethod != null) {        try {          // Bean          entity = (Entity) cauchoGetBeanMethod.invoke(value, new Object[0]);          // entity.__caucho_makePersistent(aConn, item);        } catch (Exception e) {          log.log(Level.FINER, e.toString(), e);        }      }      if (entity == null) {        throw new IllegalStateException(L.l("AmberConnection.addNewEntity unable to instantiate new entity with cauchoGetBeanMethod"));      }    }    entity.__caucho_setPrimaryKey(key);    addInternalEntity(entity);    return entity;  }  /**   * Adds a new entity for the given class name and key.   */  public Entity loadEntity(Class cl,                           Object key,                           boolean isEager)  {    if (key == null)      return null;    // jpa/0l43    Entity entity = getSubEntity(cl, key);    // If the entity is already in the context, return it    if (entity != null)      return entity;    if (_persistenceUnit.isJPA()) {      // XXX: needs to create based on the discriminator with inheritance.      // Create a new entity for the given class and primary key.      try {        entity = (Entity) load(cl, key, isEager);      } catch (AmberException e) {        throw new AmberRuntimeException(e);      }    }    else {      // HelperBean__Amber -> HelperBean      String className = cl.getSuperclass().getName();      AmberEntityHome entityHome = _persistenceUnit.getEntityHome(className);      if (entityHome == null) {        if (log.isLoggable(Level.FINER))          log.log(Level.FINER, L.l("Amber.addNewEntity: home not found for entity (class: '{0}' PK: '{1}')",                                   className, key));        return null;      }      EntityFactory factory = entityHome.getEntityFactory();      // TestBean__EJB      Object value = factory.getEntity(key);      Method cauchoGetBeanMethod = entityHome.getCauchoGetBeanMethod();      if (cauchoGetBeanMethod != null) {        try {          // Bean          entity = (Entity) cauchoGetBeanMethod.invoke(value, new Object[0]);          // entity.__caucho_makePersistent(aConn, item);        } catch (Exception e) {          log.log(Level.FINER, e.toString(), e);        }      }      if (entity == null) {        throw new IllegalStateException(L.l("AmberConnection.addNewEntity unable to instantiate new entity with cauchoGetBeanMethod"));      }      entity.__caucho_setPrimaryKey(key);      addInternalEntity(entity);    }    return entity;  }  /**   * Removes an entity.   */  public boolean removeEntity(Entity entity)  {    removeEntityImpl(entity);    if (isActiveTransaction())      removeTxEntity(entity);    return true;  }  /**   * Loads the object based on itself.   */  public boolean contains(Object obj)  {    if (obj == null)      return false;    if (! (obj instanceof Entity))      throw new IllegalArgumentException(L.l("contains() operation can only be applied to an entity instance."));    Entity entity = (Entity) obj;    // jpa/11a8    if (entity.__caucho_getConnection() != this) {      return false;    }    EntityState state = entity.__caucho_getEntityState();    if (isInTransaction() && ! state.isTransactional()) {      // jpa/11a6, jpa/1800      return false;    }    // jpa/0j5f    if (EntityState.P_DELETING.ordinal() <= state.ordinal()) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?