コード例 #1
0
  @Override
  public Object invokeMethod(String name, Object obj) {
    Object[] args = obj.getClass().isArray() ? (Object[]) obj : new Object[] {obj};

    if (isCriteriaConstructionMethod(name, args)) {

      initializeQuery();

      uniqueResult = false;

      invokeClosureNode(args[0]);

      Object result;
      if (!uniqueResult) {
        result = query.list();
      } else {
        result = query.singleResult();
      }
      query = null;
      return result;
    }

    MetaMethod metaMethod = getMetaClass().getMetaMethod(name, args);
    if (metaMethod != null) {
      return metaMethod.invoke(this, args);
    }

    metaMethod = queryMetaClass.getMetaMethod(name, args);
    if (metaMethod != null) {
      return metaMethod.invoke(query, args);
    }

    if (args.length == 1 && args[0] instanceof Closure) {

      final PersistentProperty property = persistentEntity.getPropertyByName(name);
      if (property instanceof Association) {
        Association association = (Association) property;
        Query previousQuery = query;
        PersistentEntity previousEntity = persistentEntity;

        Query associationQuery = null;
        try {
          associationQuery = query.createQuery(property.getName());
          if (associationQuery instanceof AssociationQuery) {
            previousQuery.add((Query.Criterion) associationQuery);
          }
          query = associationQuery;
          persistentEntity = association.getAssociatedEntity();
          invokeClosureNode(args[0]);
          return query;
        } finally {

          persistentEntity = previousEntity;
          query = previousQuery;
        }
      }
    }

    throw new MissingMethodException(name, getClass(), args);
  }
コード例 #2
0
 /**
  * Get hold of the GrailsDomainClassProperty represented by the targetClass' propertyName,
  * assuming targetClass corresponds to a GrailsDomainClass.
  */
 private static PersistentProperty getGrailsDomainClassProperty(
     AbstractHibernateDatastore datastore, Class<?> targetClass, String propertyName) {
   PersistentEntity grailsClass =
       datastore != null
           ? datastore.getMappingContext().getPersistentEntity(targetClass.getName())
           : null;
   if (grailsClass == null) {
     throw new IllegalArgumentException(
         "Unexpected: class is not a domain class:" + targetClass.getName());
   }
   return grailsClass.getPropertyByName(propertyName);
 }
コード例 #3
0
  protected void validatePropertyName(String propertyName, String methodName) {
    if (propertyName == null) {
      throw new IllegalArgumentException(
          "Cannot use [" + methodName + "] restriction with null property name");
    }

    PersistentProperty property = persistentEntity.getPropertyByName(propertyName);
    if (property == null && persistentEntity.getIdentity().getName().equals(propertyName)) {
      property = persistentEntity.getIdentity();
    }
    if (property == null) {
      throw new IllegalArgumentException(
          "Property ["
              + propertyName
              + "] is not a valid property of class ["
              + persistentEntity
              + "]");
    }
  }