public <T> T buildProxy(T entity, EntityOperations context, Set<Method> alreadyLoaded) {

    if (entity == null) {
      return null;
    }

    log.debug("Build Cglib proxy for entity {} ", entity);

    Class<?> proxyClass = factory.createProxyClass(entity.getClass(), context.getConfigContext());

    @SuppressWarnings("unchecked")
    T instance = (T) instantiator.instantiate(proxyClass);

    EntityMeta meta = context.getEntityMeta();
    for (PropertyMeta pm : meta.getAllMetasExceptCounters()) {
      Object value = pm.forValues().getValueFromField(entity);
      pm.forValues().setValueToField(instance, value);
    }

    for (PropertyMeta pm : meta.getAllCounterMetas()) {
      pm.forValues().setValueToField(entity, null);
    }

    ((Factory) instance)
        .setCallbacks(new Callback[] {buildInterceptor(context, entity, alreadyLoaded)});
    return instance;
  }
  public void refresh(Object proxifiedEntity, EntityOperations context)
      throws AchillesStaleObjectStateException {
    Object primaryKey = context.getPrimaryKey();
    log.debug(
        "Refreshing entity of class {} and primary key {}",
        context.getEntityClass().getCanonicalName(),
        primaryKey);

    ProxyInterceptor<Object> interceptor = proxifier.getInterceptor(proxifiedEntity);
    Object entity = context.getEntity();

    interceptor.getDirtyMap().clear();

    Object freshEntity = loader.load(context, context.getEntityClass());

    if (freshEntity == null) {
      throw new AchillesStaleObjectStateException(
          "The entity '"
              + entity
              + "' with primary_key '"
              + primaryKey
              + "' no longer exists in Cassandra");
    }
    interceptor.setTarget(freshEntity);
    interceptor.getAlreadyLoaded().clear();
    interceptor.getAlreadyLoaded().addAll(context.getAllGettersExceptCounters());
  }
 public void remove(EntityOperations context) {
   log.trace("Removing entity using PersistenceContext {}", context);
   EntityMeta entityMeta = context.getEntityMeta();
   if (entityMeta.structure().isClusteredCounter()) {
     context.bindForClusteredCounterRemoval();
   } else {
     context.bindForRemoval(entityMeta.config().getTableName());
     counterPersister.removeRelatedCounters(context);
   }
 }
  public void persist(EntityOperations context) {
    EntityMeta entityMeta = context.getEntityMeta();
    Object entity = context.getEntity();

    log.debug("Persisting transient entity {}", entity);

    if (entityMeta.structure().isClusteredCounter()) {
      counterPersister.persistClusteredCounters(context);
    } else {
      context.pushInsertStatement();
      counterPersister.persistCounters(context, entityMeta.getAllCounterMetas());
    }
  }
 public <T> T buildProxyWithAllFieldsLoadedExceptCounters(T entity, EntityOperations context) {
   return buildProxy(entity, context, context.getAllGettersExceptCounters());
 }