Find in cache.

@param <T>
the generic type
@param entityClass
the entity class
@param id
the id
@return the t
19972
@SuppressWarnings("unchecked")
    protected <T> T lookup(Class<T> entityClass, Object id)
    {
        String key = cacheKey(entityClass, id);
        LOG.debug("Reading from L1 >> " + key);
        T o = (T) sessionCache.get(key);

        // go to second-level cache
        if (o == null)
        {
            LOG.debug("Reading from L2 >> " + key);
            Cache c = (Cache) getL2Cache();
            if (c != null)
            {
                o = (T) c.get(key);
                if (o != null)
                {
                    LOG.debug("Found item in second level cache!");
                }
            }
        }
        return o;
    }
813703
@SuppressWarnings("unchecked")
	public <T> T findById(String id, Class<T> entity) {
		if (entity == null) {
			throw new ApitraryOrmException("Cannot access null entity");
		}
		if (id == null || id.isEmpty()) {
			return null;
		}

		log.debug("Searching " + entity.getName() + " " + id);

		GetRequest request = new GetRequest();
		request.setEntity(resolveApitraryEntity(entity));
		request.setId(id);
		GetResponse response = resolveApitraryClient().send(request);

		T result = (T) new GetResponseUnmarshaller(this).unMarshall(response, entity);

		if (result != null) {
			List<Field> fields = ClassUtil.getAnnotatedFields(entity, Id.class);
			if (fields.isEmpty() || fields.size() > 1) {
				throw new ApitraryOrmIdException("Illegal amount of annotated id properties of class " + entity.getClass().getName());
			} else {
				ClassUtil.setSilent(result, fields.get(0).getName(), id);
			}
		}
		return result;
	}
98122
public static @CheckForNull <T extends Descriptor> T findById(Collection<? extends T> list, String id) {
        for (T d : list) {
            if(d.getId().equals(id))
                return d;
        }
        return null;
    }
940112
public CachedDbEntity getCachedEntity(Class<?> type, String id) {
    Class<?> cacheKey = cacheKeyMapping.getEntityCacheKey(type);
    Map<String, CachedDbEntity> entitiesByType = cachedEntites.get(cacheKey);
    if(entitiesByType != null) {
      return entitiesByType.get(id);
    } else {
      return null;
    }
  }
100622
@Override
    public T findOne(final I id) {
        return inTransaction(new Callable<T>() {
            @Override
            public T call() throws Exception {
                return entityManager.find(entity, id);
            }
        });
    }
58993
public <T> T get(String organizationId, String id, Class<T> type) throws StorageException {
        T rval;
        EntityManager entityManager = getActiveEntityManager();
        try {
            OrganizationBean orgBean = entityManager.find(OrganizationBean.class, organizationId);
            Object key = new OrganizationBasedCompositeId(orgBean, id);
            rval = entityManager.find(type, key);
        } catch (Throwable t) {
            logger.error(t.getMessage(), t);
            throw new StorageException(t);
        }
        return rval;
    }
567201
public static EntityMapper getEntityMapper(Class<?> entityClass) {

        synchronized (entityClass) {
            // 先从map中获取实体映射信息
            EntityMapper entityMapper = tableMapperCache.get(entityClass);

            // 如果存在直接返回
            if (entityMapper != null) {
                return entityMapper;
            }

            TableMapper tableMapper = getTableMapper(entityClass);

            //获取实体ID泛型
            Class<?> idClass = getIdClass(entityClass);

            // 获取实体字段列表
            List<Field> fields = getAllField(entityClass);
            // 全部列
            Set<ColumnMapper> columnMapperSet = new HashSet<ColumnMapper>();
            // 主键
            ColumnMapper idColumn = null;
            GenerationType idStrategy = null;

            for (Field field : fields) {

                // 排除字段
                if (field.isAnnotationPresent(Transient.class)) {
                    continue;
                }
                ColumnMapper columnMapper = new ColumnMapper();

                // 数据库字段名
                String columnName = null;
                if (field.isAnnotationPresent(Column.class)) {
                    Column column = field.getAnnotation(Column.class);
                    columnName = column.name();
                    columnMapper.setInsertable(column.insertable());
                    columnMapper.setUpdatable(column.updatable());
                }
                // 如果为空，使用属性名并替换为下划线风格
                if (columnName == null || columnName.equals("")) {
                    columnName = camelhumpToUnderline(field.getName());
                }

                columnMapper.setProperty(field.getName());
                columnMapper.setColumn(columnName);
                columnMapper.setJavaType(field.getType());

                // 是否主键
                if(field.isAnnotationPresent(Id.class)){                	
                	columnMapper.setId(true);
                	if(field.isAnnotationPresent(GeneratedValue.class)){ 
                		idStrategy = field.getAnnotation(GeneratedValue.class).strategy();
                	}
                	idColumn = columnMapper;
                }
                // 添加到所有字段映射信息
                columnMapperSet.add(columnMapper);

            }
            if (columnMapperSet.size() <= 0) {
                throw new RuntimeException("实体" + entityClass.getName() + "不存在映射字段");
            }
            if (idColumn == null) {
                throw new RuntimeException("实体" + entityClass.getName() + "不存在主键");
            }

            // 解析实体映射信息
            entityMapper = new EntityMapper();
            entityMapper.setTableMapper(tableMapper);
            entityMapper.setColumnsMapper(columnMapperSet);
            entityMapper.setIdClass(idClass);
            entityMapper.setIdColumn(idColumn);
            entityMapper.setIdStrategy(idStrategy);

            tableMapperCache.put(entityClass, entityMapper);

            return entityMapper;
        }
    }
248793
public IBasicEntity get(EntityIdentifier entityID) throws CachingException {
        return EntityCachingServiceLocator.getEntityCachingService()
                .get(entityID.getType(), entityID.getKey());
    }
465646
public <E> List<E> find(Class<E> entityClass, Object... primaryKeys)
    {
        List<E> entities = new ArrayList<E>();
        if (primaryKeys == null)
        {
            return entities;
        }
        Set<Object> pKeys = new HashSet<Object>(Arrays.asList(primaryKeys));
        for (Object primaryKey : pKeys)
        {
            E e = find(entityClass, primaryKey);
            if (e != null)
            {
                entities.add(e);
            }
        }
        return entities;
    }
709250
@Override
    public <E> List<E> find(Class<E> entityClass, Map<String, String> col)
    {
        EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, getPersistenceUnit(),
                entityClass);
        List<E> entities = new ArrayList<E>();
        Map<String, Field> columnFamilyNameToFieldMap = MetadataUtils.createSuperColumnsFieldMap(entityMetadata,
                kunderaMetadata);
        for (String columnFamilyName : col.keySet())
        {
            String entityId = col.get(columnFamilyName);
            if (entityId != null)
            {
                E e = null;
                try
                {
                    List results = new ArrayList();
                    fetchEntity(entityClass, entityId, entityMetadata, entityMetadata.getRelationNames(),
                            entityMetadata.getSchema(), results, null, null);
                    if (results != null)
                    {
                        e = (E) results.get(0);
                    }
                }
                catch (IOException ioex)
                {
                    log.error("Error during find for embedded entities, Caused by: .", ioex);

                    throw new KunderaException(ioex);
                }

                Field columnFamilyField = columnFamilyNameToFieldMap.get(columnFamilyName.substring(0,
                        columnFamilyName.indexOf("|")));
                Object columnFamilyValue = PropertyAccessorHelper.getObject(e, columnFamilyField);
                if (Collection.class.isAssignableFrom(columnFamilyField.getType()))
                {
                    entities.addAll((Collection) columnFamilyValue);
                }
                else
                {
                    entities.add((E) columnFamilyValue);
                }
            }
        }
        return entities;
    }
190299
public Object find(Class entityClass, Object key)
    {

        EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClass);

        String tableName = entityMetadata.getTableName();
        Class fieldClass = entityMetadata.getIdAttribute().getJavaType();
        String select_Query = SparkQueryConstants.SELECTALL_QUERY;
        select_Query = StringUtils.replace(select_Query, SparkQueryConstants.TABLE, tableName);
        StringBuilder builder = new StringBuilder(select_Query);
        builder.append(SparkQueryConstants.ADD_WHERE_CLAUSE);
        builder.append(dataHandler.getColumnName(entityMetadata.getIdAttribute()));
        builder.append(SparkQueryConstants.EQUALS);

        appendValue(builder, fieldClass, key);
        List result = this.executeQuery(builder.toString(), entityMetadata, null);
        return result.isEmpty() ? null : result.get(0);
    }
686536
@Override
    public Object find(Class entityClass, Object key)
    {
        GraphDatabaseService graphDb = null;
        if (resource != null)
        {
            graphDb = getConnection();
        }

        if (graphDb == null)
            graphDb = factory.getConnection();

        EntityMetadata m = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClass);

        Object entity = null;
        Node node = mapper.searchNode(key, m, graphDb, true);

        if (node != null && !((Neo4JTransaction) resource).containsNodeId(node.getId()))

        {
            entity = getEntityWithAssociationFromNode(m, node);
        }

        return entity;
    }
620401
public IdClass<Entity<T>> getOrCreateIdClass()
   {
      Node node = childNode.getOrCreate("id-class");
      IdClass<Entity<T>> idClass = new IdClassImpl<Entity<T>>(this, "id-class", childNode, node);
      return idClass;
   }
175647
public static <T> T queryTryCache(Class<? extends BaseEntity> entityClass,String key,long expireSeconds,Callable<T> dataCaller){
		
		if(CacheHandler.cacheProvider == null){
			try {
				return dataCaller.call();
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		
		String entityClassName = entityClass.getSimpleName();
		key = entityClassName + CacheHandler.SPLIT_PONIT + key;
		T result = CacheHandler.cacheProvider.get(key);
		if(result == null){
			try {				
				result = dataCaller.call();
				if(result != null){
					CacheHandler.cacheProvider.set(key, result, expireSeconds);
					String cacheGroupKey = entityClassName + CacheHandler.GROUPKEY_SUFFIX;
					CacheHandler.cacheProvider.putGroup(cacheGroupKey, key, expireSeconds);
				}
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return result;
	}
1145201
protected <T extends HistoryEvent> T findInCache(Class<T> type, String id) {
    return Context.getCommandContext()
      .getDbEntityManager()
      .getCachedEntity(type, id);
  }
198570
public static Class<? extends Tag> getClassFor(int id) {
        if(!idToTag.containsKey(id)) {
            return null;
        }

        return idToTag.get(id);
    }
515087
public <E> List<E> findAll(Class<E> entityClass, String[] columnsToSelect, Object... rowIds) {
        EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClass);
        List<E> results = new ArrayList<E>();
        results = find(entityClass, entityMetadata.getRelationNames(),
            entityMetadata.getRelationNames() != null && !entityMetadata.getRelationNames().isEmpty(), entityMetadata,
            rowIds);
        return results.isEmpty() ? null : results;
    }