@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;
  }
 private static String getFamily(
     PersistentEntity persistentEntity, SimpleDBDomainClassMappedForm mappedForm) {
   String table = null;
   if (mappedForm != null) {
     table = mappedForm.getFamily();
   }
   if (table == null) table = persistentEntity.getJavaClass().getSimpleName();
   return table;
 }
  @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
  protected List<Object> retrieveAllEntities(
      PersistentEntity persistentEntity, Iterable<Serializable> keys) {

    Query query = session.createQuery(persistentEntity.getJavaClass());

    PersistentProperty identity = persistentEntity.getIdentity();
    if (keys instanceof List) {
      List actualKeys = new ArrayList();
      Iterator iterator = keys.iterator();
      while (iterator.hasNext()) {
        Object key = iterator.next();
        Object id = getIdentifierForKey(key);
        actualKeys.add(id);
      }
      query.in(identity.getName(), actualKeys);
    } else {
      List<Serializable> keyList = new ArrayList<Serializable>();
      for (Serializable key : keys) {
        keyList.add(key);
      }
      query.in(identity.getName(), keyList);
    }

    List<Object> entityResults = new ArrayList<Object>();
    Iterator<Serializable> keyIterator = keys.iterator();
    Map<Serializable, Object> resultMap = new HashMap<Serializable, Object>();
    for (Object o : query.list()) {
      if (o instanceof DBObject) {
        DBObject dbo = (DBObject) o;
        o =
            createObjectFromNativeEntry(
                getPersistentEntity(), (Serializable) dbo.get(MONGO_ID_FIELD), dbo);
      }
      resultMap.put(getObjectIdentifier(o), o);
    }
    while (keyIterator.hasNext()) {
      Object key = getIdentifierForKey(keyIterator.next());
      ConversionService conversionService = getMappingContext().getConversionService();
      key = conversionService.convert(key, identity.getType());
      Object o = resultMap.get(key);
      entityResults.add(o); // may add null, so entityResults list size matches input list size.
    }

    return entityResults;
  }
  public static org.hibernate.criterion.DetachedCriteria getHibernateDetachedCriteria(
      AbstractHibernateQuery hibernateQuery, QueryableCriteria<?> queryableCriteria) {

    String alias = queryableCriteria.getAlias();
    PersistentEntity persistentEntity = queryableCriteria.getPersistentEntity();
    Class targetClass = persistentEntity.getJavaClass();
    org.hibernate.criterion.DetachedCriteria detachedCriteria;

    if (alias != null) {
      detachedCriteria = org.hibernate.criterion.DetachedCriteria.forClass(targetClass, alias);
    } else {
      detachedCriteria = org.hibernate.criterion.DetachedCriteria.forClass(targetClass);
    }
    populateHibernateDetachedCriteria(
        new HibernateQuery(detachedCriteria, persistentEntity),
        detachedCriteria,
        queryableCriteria);
    return detachedCriteria;
  }
 public boolean isOwningEntity(PersistentEntity owner) {
   return domainClass.isOwningClass(owner.getJavaClass());
 }
 /**
  * @see #getPersistentProperties(Class, org.grails.datastore.mapping.model.MappingContext,
  *     org.grails.datastore.mapping.model.ClassMapping)
  */
 public List<PersistentProperty> getPersistentProperties(
     PersistentEntity entity, MappingContext context, ClassMapping classMapping) {
   return getPersistentProperties(entity.getJavaClass(), context, classMapping);
 }