@Override
  protected void setManyToMany(
      PersistentEntity persistentEntity,
      Object obj,
      DBObject nativeEntry,
      ManyToMany manyToMany,
      Collection associatedObjects,
      Map<Association, List<Serializable>> toManyKeys) {

    List ids = new ArrayList();
    if (associatedObjects != null) {
      for (Object o : associatedObjects) {
        if (o == null) {
          ids.add(null);
        } else {
          PersistentEntity childPersistentEntity =
              getMappingContext().getPersistentEntity(o.getClass().getName());
          EntityAccess entityAccess = createEntityAccess(childPersistentEntity, o);
          ids.add(entityAccess.getIdentifier());
        }
      }
    }

    nativeEntry.put(manyToMany.getName() + "_$$manyToManyIds", ids);
  }
 @Override
 protected Collection getManyToManyKeys(
     PersistentEntity persistentEntity,
     Object object,
     Serializable nativeKey,
     DBObject nativeEntry,
     ManyToMany manyToMany) {
   return (Collection) nativeEntry.get(manyToMany.getName() + "_$$manyToManyIds");
 }
  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));
          }
        }
      }
    }
  }