/**
  * Returns <code>true</code> if the entity name needs to be filtered.
  *
  * <p>Implementation filteres out inherited hibernate mappings, since the select query for the
  * base class will cover any inherited classes as well.
  *
  * <p>Note, that this method is called after it has been verified that the class has Compass
  * mappings (either directly, or indirectly by an interface or a super class).
  *
  * @param entityname The name of the entity
  * @param classMetadata The Hibernate class meta data.
  * @param device The Jpa Gps device
  * @return <code>true</code> if the entity should be filtered out, <code>false</code> if not.
  */
 protected boolean shouldFilter(
     String entityname, ClassMetadata classMetadata, Map allClassMetaData, JpaGpsDevice device) {
   Class<?> clazz = classMetadata.getMappedClass();
   // if it is inherited, do not add it to the classes to index, since the "from [entity]"
   // query for the base class will return results for this class as well
   if (classMetadata.isInherited()) {
     String superClassEntityName = ((AbstractEntityPersister) classMetadata).getMappedSuperclass();
     ClassMetadata superClassMetadata = (ClassMetadata) allClassMetaData.get(superClassEntityName);
     Class superClass = superClassMetadata.getMappedClass();
     // only filter out classes that their super class has compass mappings
     if (superClass != null
         && ((CompassGpsInterfaceDevice) device.getGps())
             .hasMappingForEntityForIndex(superClass)) {
       if (log.isDebugEnabled()) {
         log.debug(
             "Entity ["
                 + entityname
                 + "] is inherited and super class ["
                 + superClass
                 + "] has compass mapping, filtering it out");
       }
       return true;
     }
   }
   return false;
 }
Пример #2
0
    /**
     * Takes a class name as a string and find that class and all of its subclasses (transitively)
     * returns them as a list.
     */
    private static List<Class<?>> hierarchy(Map<String, ClassMetadata> m, String key) {

      final List<Class<?>> h = new ArrayList<Class<?>>();

      ClassMetadata cm = m.get(key);
      Class<?> c = cm.getMappedClass(EntityMode.POJO);
      h.add(c);

      int index = 0;

      while (index < h.size()) {
        for (String key2 : m.keySet()) {
          if (key.equals(key2)) {
            continue;
          } else {
            cm = m.get(key2);
            c = cm.getMappedClass(EntityMode.POJO);
            if (c.getSuperclass().equals(h.get(index))) {
              h.add(c);
            }
          }
        }
        index++;
      }
      return h;
    }
 protected void setRelatedClassType(
     GrailsHibernateDomainClassProperty prop, AssociationType assType, Type hibernateType) {
   try {
     String associatedEntity =
         assType.getAssociatedEntityName((SessionFactoryImplementor) getSessionFactory());
     ClassMetadata associatedMetaData = getSessionFactory().getClassMetadata(associatedEntity);
     prop.setRelatedClassType(associatedMetaData.getMappedClass(EntityMode.POJO));
   } catch (MappingException me) {
     // other side must be a value object
     if (hibernateType.isCollectionType()) {
       prop.setRelatedClassType(Collection.class);
     }
   }
 }
  public EntityInformation[] locate(EntityManagerFactory entityManagerFactory, JpaGpsDevice device)
      throws JpaGpsDeviceException {

    CompassGpsInterfaceDevice gps = (CompassGpsInterfaceDevice) device.getGps();

    HibernateEntityManagerFactory hibernateEntityManagerFactory =
        (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = hibernateEntityManagerFactory.getSessionFactory();

    ArrayList<EntityInformation> entitiesList = new ArrayList<EntityInformation>();

    Map allClassMetaData = sessionFactory.getAllClassMetadata();
    for (Object o : allClassMetaData.keySet()) {
      String entityname = (String) o;
      if (!gps.hasMappingForEntityForIndex((entityname))) {
        if (log.isDebugEnabled()) {
          log.debug("Entity [" + entityname + "] does not have compass mapping, filtering it out");
        }
        continue;
      }

      ClassMetadata classMetadata = (ClassMetadata) allClassMetaData.get(entityname);
      if (shouldFilter(entityname, classMetadata, allClassMetaData, device)) {
        continue;
      }
      Class<?> clazz = classMetadata.getMappedClass();
      ResourceMapping resourceMapping = gps.getMappingForEntityForIndex(entityname);
      EntityInformation entityInformation =
          new EntityInformation(
              clazz,
              entityname,
              new HibernateJpaQueryProvider(clazz, entityname),
              resourceMapping.getSubIndexHash().getSubIndexes());
      entitiesList.add(entityInformation);
      if (log.isDebugEnabled()) {
        log.debug("Entity [" + entityname + "] will be indexed");
      }
    }
    return entitiesList.toArray(new EntityInformation[entitiesList.size()]);
  }
Пример #5
0
    /**
     * Initializes the metadata needed by this instance.
     *
     * @param sessionFactory
     * @see SessionFactory#getAllClassMetadata()
     */
    public void setSessionFactory(SessionFactory sessionFactory) {

      if (initialized) {
        return; // EARLY EXIT !!
      }

      log.info("Calculating ExtendedMetadata...");

      SessionFactoryImplementor sfi = (SessionFactoryImplementor) sessionFactory;
      Map<String, ClassMetadata> m = sessionFactory.getAllClassMetadata();

      // do Locks() first because they are used during the
      // calculation of LockedBy()
      for (String key : m.keySet()) {
        ClassMetadata cm = m.get(key);
        locksHolder.put(key, new Locks(cm));
      }

      // now that all Locks() are available, determine LockedBy()
      for (String key : m.keySet()) {
        lockedByHolder.put(key, lockedByFields(key, m));
      }

      for (String key : m.keySet()) {
        ClassMetadata cm = m.get(key);
        immutablesHolder.put(key, new Immutables(cm));
      }

      for (Map.Entry<String, ClassMetadata> entry : m.entrySet()) {
        String key = entry.getKey();
        ClassMetadata cm = entry.getValue();
        key = key.substring(key.lastIndexOf(".") + 1).toLowerCase();
        if (hibernateClasses.containsKey(key)) {
          throw new RuntimeException("Duplicate keys!: " + key);
        }
        hibernateClasses.put(key, cm.getMappedClass(EntityMode.POJO));
      }

      for (String key : m.keySet()) {
        Map<String, Relationship> value = new HashMap<String, Relationship>();
        ClassMetadata cm = m.get(key);
        for (Class<?> c : hierarchy(m, key)) {
          Locks locks = locksHolder.get(c.getName());
          locks.fillRelationships(sfi, value);
        }

        // FIXME: using simple name rather than FQN
        Map<String, Relationship> value2 = new HashMap<String, Relationship>();
        for (Map.Entry<String, Relationship> i : value.entrySet()) {
          String k = i.getKey();
          k = k.substring(k.lastIndexOf(".") + 1);
          value2.put(k, i.getValue());
        }
        relationships.put(key.substring(key.lastIndexOf(".") + 1), value2);
      }

      Set<Class<IAnnotated>> anns = new HashSet<Class<IAnnotated>>();
      Set<Class<Annotation>> anns2 = new HashSet<Class<Annotation>>();
      for (String key : m.keySet()) {
        ClassMetadata cm = m.get(key);
        Map<String, String> queries = countQueriesAndEditTargets(key, lockedByHolder.get(key));
        collectionCountHolder.putAll(queries);

        // Checking classes, specifically for ITypes
        Class c = cm.getMappedClass(EntityMode.POJO);
        if (IAnnotated.class.isAssignableFrom(c)) {
          anns.add(c);
        }
        if (Annotation.class.isAssignableFrom(c)) {
          anns2.add(c);
        }
      }
      annotatableTypes.addAll(anns);
      annotationTypes.addAll(anns2);
      initialized = true;
    }
 /** This implementation only supports BeanWithId objects with integer ids */
 public Object instantiate(String entityName, EntityMode entityMode, Serializable id) {
   ClassMetadata classMetadata = m_sessionFactory.getClassMetadata(entityName);
   Class clazz = classMetadata.getMappedClass(entityMode);
   return instantiate(clazz, id);
 }