@Autowired
 public void postConstruct(SessionFactory sessionFactory) {
   this.sessionFactory = sessionFactory;
   SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) this.sessionFactory;
   sessionFactoryImpl
       .getServiceRegistry()
       .getService(EventListenerRegistry.class)
       .appendListeners(EventType.POST_UPDATE, this);
 }
  public void close() {
    // The spec says so, that's why :(
    validateNotClosed();

    sessionFactory.close();
    EntityManagerFactoryRegistry.INSTANCE.removeEntityManagerFactory(
        entityManagerFactoryName, this);
  }
  @Override
  public void addNamedQuery(String name, Query query) {
    validateNotClosed();

    // NOTE : we use Query#unwrap here (rather than direct type checking) to account for possibly
    // wrapped
    // query implementations

    // first, handle StoredProcedureQuery
    try {
      final StoredProcedureQueryImpl unwrapped = query.unwrap(StoredProcedureQueryImpl.class);
      if (unwrapped != null) {
        addNamedStoredProcedureQuery(name, unwrapped);
        return;
      }
    } catch (PersistenceException ignore) {
      // this means 'query' is not a StoredProcedureQueryImpl
    }

    // then try as a native-SQL or JPQL query
    try {
      final HibernateQuery unwrapped = query.unwrap(HibernateQuery.class);
      if (unwrapped != null) {
        // create and register the proper NamedQueryDefinition...
        final org.hibernate.Query hibernateQuery = ((HibernateQuery) query).getHibernateQuery();
        if (org.hibernate.SQLQuery.class.isInstance(hibernateQuery)) {
          sessionFactory.registerNamedSQLQueryDefinition(
              name, extractSqlQueryDefinition((org.hibernate.SQLQuery) hibernateQuery, name));
        } else {
          sessionFactory.registerNamedQueryDefinition(
              name, extractHqlQueryDefinition(hibernateQuery, name));
        }
        return;
      }
    } catch (PersistenceException ignore) {
      // this means 'query' is not a native-SQL or JPQL query
    }

    // if we get here, we are unsure how to properly unwrap the incoming query to extract the needed
    // information
    throw new PersistenceException(
        String.format(
            "Unsure how to how to properly unwrap given Query [%s] as basis for named query",
            query));
  }
Exemplo n.º 4
0
  /** @param connectionProvider */
  public static void stopConnectionProvider(SessionFactoryImpl sessionFactory) {
    if (sessionFactory == null) {
      return;
    }

    try {
      sessionFactory.close();

    } catch (Exception e) {
      e.printStackTrace();
    }

    ConnectionProvider connectionProvider = sessionFactory.getConnectionProvider();
    if (connectionProvider == null) {
      return;
    }

    Method method = KernelReflect.declaredMethod(connectionProvider.getClass(), "close");
    if (method == null) {
      method = KernelReflect.declaredMethod(connectionProvider.getClass(), "stop");
      if (method == null) {
        method = KernelReflect.declaredMethod(connectionProvider.getClass(), "destory");
      }
    }

    if (method == null) {
      LOGGER.info("stop " + connectionProvider + " failed");

    } else {
      try {
        LOGGER.info("stop " + connectionProvider + " at " + method.getName());
        method.invoke(connectionProvider);

      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    Object ds = KernelObject.declaredGet(connectionProvider, "ds");
    if (ds != null) {
      KernelObject.declaredSend(ds, "close", true);
    }
  }
Exemplo n.º 5
0
  @SuppressWarnings({CompilerWarnings.UNCHECKED})
  private void buildEntityMetadatas() throws Exception {
    SessionFactoryImpl sessionFactory =
        ((SessionFactoryImpl) this.entityManagerFactory.getSessionFactory());
    Class<?> entityBindingMappedClass;
    Class<? extends SdcctEntity> entityMappedClass;
    String entityName, entityPropName, entityPropFieldName;
    EntityMetadata entityMetadata;
    Map<String, PropertyMetadata> entityPropMetadatas;
    IndexedTypeDescriptor indexedEntityDesc;
    Method entityPropGetterMethod;
    boolean entityIndexed, entityPropIndexed;
    PropertyDescriptor indexedEntityPropDesc;
    PropertyMetadata entityPropMetadata;
    BidiMap<Integer, String> entityPropOrder;
    String[] entityPropOrderNames;

    for (PersistentClass entityBinding : metadata.getEntityBindings()) {
      if (((entityBindingMappedClass = entityBinding.getMappedClass()) == null)
          || !SdcctEntity.class.isAssignableFrom(entityBindingMappedClass)) {
        continue;
      }

      entityPropMetadatas =
          (entityMetadata =
                  new EntityMetadataImpl(
                      (entityName = entityBinding.getEntityName()),
                      (entityIndexed =
                          (indexedEntityDesc =
                                  searchIntegrator.getIndexedTypeDescriptor(
                                      (entityMappedClass =
                                          ((Class<? extends SdcctEntity>)
                                              entityBindingMappedClass))))
                              .isIndexed()),
                      entityMappedClass,
                      entityBinding.getTable().getName()))
              .getProperties();

      this.entityMetadatas.put(
          ((Class<? extends SdcctEntity>) entityMappedClass.getInterfaces()[0]), entityMetadata);

      for (Property entityProp :
          IteratorUtils.asIterable(
              IteratorUtils.chainedIterator(
                  ((Iterator<Property>) entityBinding.getRootClass().getPropertyIterator()),
                  ((Iterator<Property>) entityBinding.getPropertyIterator())))) {
        entityPropName = entityProp.getName();
        entityPropGetterMethod = entityProp.getGetter(entityMappedClass).getMethod();

        if (entityProp.getColumnSpan() == 0) {
          continue;
        }

        entityPropIndexed =
            (entityIndexed
                && entityPropGetterMethod.isAnnotationPresent(Fields.class)
                && ((indexedEntityPropDesc = indexedEntityDesc.getProperty(entityPropName)) != null)
                && !indexedEntityPropDesc.isId());

        entityPropMetadatas.put(
            entityPropName,
            (entityPropMetadata =
                new PropertyMetadataImpl(
                    entityPropName,
                    entityPropIndexed,
                    ((Column) entityProp.getColumnIterator().next()).getName(),
                    entityProp.getType())));

        if (entityPropIndexed) {
          for (Field entityPropFieldAnno :
              entityPropGetterMethod.getAnnotation(Fields.class).value()) {
            if (entityPropFieldAnno.analyze() == Analyze.NO) {
              continue;
            }

            entityPropFieldName = entityPropFieldAnno.name();

            switch (entityPropFieldAnno.analyzer().definition()) {
              case DbAnalyzerNames.EDGE_NGRAM:
                entityPropMetadata.setEdgeNgramFieldName(entityPropFieldName);
                break;

              case DbAnalyzerNames.LOWERCASE:
                entityPropMetadata.setLowercaseFieldName(entityPropFieldName);
                break;

              case DbAnalyzerNames.NGRAM:
                entityPropMetadata.setNgramFieldName(entityPropFieldName);
                break;

              case DbAnalyzerNames.PHONETIC:
                entityPropMetadata.setPhoneticFieldName(entityPropFieldName);
                break;
            }
          }
        }
      }

      entityMetadata.setIdProperty(
          entityPropMetadatas.get(entityBinding.getIdentifierProperty().getName()));

      entityPropOrder = entityMetadata.getPropertyOrder();
      entityPropOrderNames = sessionFactory.getEntityPersister(entityName).getPropertyNames();

      for (int a = 0; a < entityPropOrderNames.length; a++) {
        entityPropOrder.put(a, entityPropOrderNames[a]);
      }
    }

    LOGGER.debug(
        String.format(
            "Processed metadata for %d entities: [%s]",
            this.entityMetadatas.size(),
            this.entityMetadatas
                .values()
                .stream()
                .map(NamedBean::getName)
                .collect(Collectors.joining(", "))));
  }
 public boolean isOpen() {
   return !sessionFactory.isClosed();
 }
 private void addNamedStoredProcedureQuery(String name, StoredProcedureQueryImpl query) {
   final ProcedureCall procedureCall = query.getHibernateProcedureCall();
   sessionFactory
       .getNamedQueryRepository()
       .registerNamedProcedureCallMemento(name, procedureCall.extractMemento(query.getHints()));
 }