Пример #1
0
 public boolean isOneToOneCascadeAll(Method method) throws Exception {
   Boolean isOneToOneAll = mappedOneToOneCascadeAll.get(method);
   if (isOneToOneAll != null) {
     return isOneToOneAll;
   }
   OneToOne oneToOne = method.getAnnotation(OneToOne.class);
   if (oneToOne == null) {
     Field field = getDeclaredField(method.getDeclaringClass(), getMethodName(method));
     if (field != null) {
       oneToOne = field.getAnnotation(OneToOne.class);
     }
   }
   CascadeType[] cascadeTypes = null;
   if (oneToOne != null) {
     cascadeTypes = oneToOne.cascade();
   }
   if (oneToOne != null
       && cascadeTypes != null
       && cascadeTypes.length > 0
       && Arrays.asList(cascadeTypes).contains(CascadeType.ALL)) {
     isOneToOneAll = true;
   } else {
     isOneToOneAll = false;
   }
   mappedOneToOneCascadeAll.put(method, isOneToOneAll);
   return isOneToOneAll;
 }
Пример #2
0
 /**
  * 一対一の関連を処理します。
  *
  * @param propertyMeta プロパティメタデータ
  * @param field フィールド
  * @param entityMeta エンティティメタデータ
  * @param oneToOne 一対一関連
  */
 protected void doOneToOne(
     PropertyMeta propertyMeta, Field field, EntityMeta entityMeta, OneToOne oneToOne) {
   propertyMeta.setRelationshipType(RelationshipType.ONE_TO_ONE);
   Class<?> relationshipClass = field.getType();
   if (relationshipClass.getAnnotation(Entity.class) == null) {
     throw new RelationshipNotEntityRuntimeException(
         entityMeta.getName(), propertyMeta.getName(), relationshipClass);
   }
   propertyMeta.setRelationshipClass(relationshipClass);
   String mappedBy = oneToOne.mappedBy();
   if (!StringUtil.isEmpty(mappedBy)) {
     if (propertyMeta.getJoinColumnMetaList().size() > 0) {
       throw new BothMappedByAndJoinColumnRuntimeException(
           entityMeta.getName(), propertyMeta.getName());
     }
     propertyMeta.setMappedBy(mappedBy);
   }
 }
  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));
          }
        }
      }
    }
  }