@Override
  @SuppressWarnings({"rawtypes", "unchecked"})
  public Family createMappedForm(PersistentEntity entity) {
    ClassPropertyFetcher cpf = ClassPropertyFetcher.forClass(entity.getJavaClass());
    final Closure value = cpf.getStaticPropertyValue(GormProperties.MAPPING, Closure.class);
    if (value == null) {
      return new Region();
    }

    final Region family = new Region();
    AttributesFactory factory =
        new AttributesFactory() {
          @SuppressWarnings("unused")
          public void setRegion(String name) {
            family.setRegion(name);
          }
        };
    factory.setDataPolicy(defaultDataPolicy);

    MappingConfigurationBuilder builder = new MappingConfigurationBuilder(factory, KeyValue.class);
    builder.evaluate(value);
    entityToPropertyMap.put(entity, builder.getProperties());
    final RegionAttributes regionAttributes = factory.create();
    family.setRegionAttributes(regionAttributes);
    family.setCacheListeners(regionAttributes.getCacheListeners());
    family.setDataPolicy(regionAttributes.getDataPolicy());
    family.setCacheLoader(regionAttributes.getCacheLoader());
    family.setCacheWriter(regionAttributes.getCacheWriter());

    builder = new MappingConfigurationBuilder(family, KeyValue.class);
    builder.evaluate(value);
    return family;
  }
  @SuppressWarnings("unchecked")
  @Override
  public Table createMappedForm(PersistentEntity entity) {
    Table table = super.createMappedForm(entity);
    CassandraPersistentEntity cassandraPersistentEntity = (CassandraPersistentEntity) entity;
    // read tableOptions
    ClassPropertyFetcher cpf = ClassPropertyFetcher.forClass(entity.getJavaClass());
    final Closure value = cpf.getStaticPropertyValue(TABLE_PROPERTIES, Closure.class);
    if (value != null) {
      MapConfigurationBuilder builder = new MapConfigurationBuilder();
      try {
        builder.evaluate(value);
      } catch (Exception e) {
        throw new IllegalMappingException(
            String.format("Error reading %s : %s", TABLE_PROPERTIES, e.toString()));
      }
      table.setTableProperties(builder.getProperties());
    }

    if (table.getKeyspace() == null) {
      table.setKeyspace(keyspace);
    }

    // additional static mapping block handling
    Map<String, Column> properties = entityToPropertyMap.get(entity);
    Object version = properties.get(MappingConfigurationBuilder.VERSION_KEY);
    if (version instanceof Boolean) {
      cassandraPersistentEntity.setVersion((Boolean) version);
    }

    Column idProperty = properties.get(IDENTITY_PROPERTY);
    Iterator<Entry<String, Column>> propertyIterator = properties.entrySet().iterator();

    while (propertyIterator.hasNext()) {
      Entry<String, Column> entry = propertyIterator.next();
      if (entry.getValue() instanceof Column) {
        String name = entry.getKey();
        Column column = entry.getValue();
        if (idProperty != null
            && idProperty.getName() != null
            && idProperty.getName().equals(name)) {
          // remove extra column created if id property in constraints block, as it conflicts with
          // the column created in mapping block.
          // constraints will be handled elsewhere in GORM
          propertyIterator.remove();
          continue;
        }

        if (column.getName() == null) {
          column.setName(name);
        }
        table.addColumn(column);
      }
    }

    return table;
  }
 @Override
 public Serializable getObjectIdentifier(Object instance) {
   if (instance == null) return null;
   if (proxyHandler.isProxy(instance)) {
     return ((HibernateProxy) instance).getHibernateLazyInitializer().getIdentifier();
   }
   Class<?> type = instance.getClass();
   ClassPropertyFetcher cpf = ClassPropertyFetcher.forClass(type);
   final PersistentEntity persistentEntity =
       getMappingContext().getPersistentEntity(type.getName());
   if (persistentEntity != null) {
     return (Serializable)
         cpf.getPropertyValue(instance, persistentEntity.getIdentity().getName());
   }
   return null;
 }
  public void initializeClassMapping(
      Class javaClass, MappingContext context, ClassMapping mapping) {
    if (properties.containsKey(javaClass)) {
      return;
    }

    List<PersistentProperty> persistentProperties = new ArrayList<PersistentProperty>();
    Set<Class> owners = new HashSet<Class>();

    properties.put(javaClass, persistentProperties);
    owningEntities.put(javaClass, owners);

    final ClassPropertyFetcher cpf = ClassPropertyFetcher.forClass(javaClass);
    final PersistentEntity owner = getPersistentEntity(javaClass, context, mapping);

    for (PropertyDescriptor propertyDescriptor : cpf.getPropertyDescriptors()) {
      if (propertyDescriptor.getReadMethod() != null
          && propertyDescriptor.getWriteMethod() != null) {
        Field field;
        try {
          field = cpf.getDeclaredField(propertyDescriptor.getName());
        } catch (Exception e) {
          continue;
        }
        if (field != null) {
          if (field.getAnnotation(Basic.class) != null
              || field.getAnnotation(Temporal.class) != null
              || field.getAnnotation(Version.class) != null) {
            persistentProperties.add(
                propertyFactory.createSimple(owner, context, propertyDescriptor));
          } else if (field.getAnnotation(Id.class) != null) {
            identities.put(
                javaClass, propertyFactory.createIdentity(owner, context, propertyDescriptor));
          } else if (field.getAnnotation(Embedded.class) != null) {
            final org.grails.datastore.mapping.model.types.Embedded embeddedProperty =
                propertyFactory.createEmbedded(owner, context, propertyDescriptor);
            embeddedProperty.setAssociatedEntity(
                getOrCreateAssociatedEntity(context, field.getType()));
            persistentProperties.add(embeddedProperty);
          } else if (field.getAnnotation(OneToOne.class) != null) {
            OneToOne one2one = field.getAnnotation(OneToOne.class);

            if (one2one.mappedBy() != null && one2one.targetEntity() != null) {
              owners.add(one2one.targetEntity());
            }
            final ToOne oneToOneProperty =
                propertyFactory.createOneToOne(owner, context, propertyDescriptor);
            oneToOneProperty.setAssociatedEntity(
                getOrCreateAssociatedEntity(context, field.getType()));
            persistentProperties.add(oneToOneProperty);
          } else if (field.getAnnotation(OneToMany.class) != null) {
            OneToMany one2m = field.getAnnotation(OneToMany.class);

            if (one2m.mappedBy() != null && one2m.targetEntity() != null) {
              owners.add(one2m.targetEntity());
            }
            final org.grails.datastore.mapping.model.types.OneToMany oneToManyProperty =
                propertyFactory.createOneToMany(owner, context, propertyDescriptor);
            oneToManyProperty.setAssociatedEntity(
                getOrCreateAssociatedEntity(context, one2m.targetEntity()));
            persistentProperties.add(oneToManyProperty);
          } else if (field.getAnnotation(ManyToMany.class) != null) {
            ManyToMany m2m = field.getAnnotation(ManyToMany.class);

            if (m2m.mappedBy() != null && m2m.targetEntity() != null) {
              owners.add(m2m.targetEntity());
            }
            final org.grails.datastore.mapping.model.types.ManyToMany manyToManyProperty =
                propertyFactory.createManyToMany(owner, context, propertyDescriptor);
            manyToManyProperty.setAssociatedEntity(
                getOrCreateAssociatedEntity(context, m2m.targetEntity()));
            persistentProperties.add(manyToManyProperty);
          } else if (field.getAnnotation(ManyToOne.class) != null) {
            final ToOne manyToOneProperty =
                propertyFactory.createManyToOne(owner, context, propertyDescriptor);
            manyToOneProperty.setAssociatedEntity(
                getOrCreateAssociatedEntity(context, field.getType()));
            persistentProperties.add(manyToOneProperty);
          } else if (field.getAnnotation(Transient.class) == null) {
            persistentProperties.add(
                propertyFactory.createSimple(owner, context, propertyDescriptor));
          }
        }
      }
    }
  }