Package org.hibernate

Examples of org.hibernate.SessionFactory


  @Override
  public void index(String entity) {
    Class<?> clazz = getEntityClass( entity );

    SessionFactory factory = getSessionFactory();
    Session session = factory.openSession();
    FullTextSession fulltextSession = Search.getFullTextSession( session );
    try {
      fulltextSession.createIndexer( clazz )
          .batchSizeToLoadObjects( batchSize )
          .cacheMode( CacheMode.NORMAL )
View Full Code Here


  @Override
  public void optimize(String entity) {
    Class<?> clazz = getEntityClass( entity );

    SessionFactory factory = getSessionFactory();
    Session session = factory.openSession();
    FullTextSession fullTextSession = Search.getFullTextSession( session );
    fullTextSession.beginTransaction();
    fullTextSession.getSearchFactory().optimize( clazz );
    fullTextSession.getTransaction().commit();
    session.close();
View Full Code Here

  @Override
  public void purge(String entity) {
    Class<?> clazz = getEntityClass( entity );

    SessionFactory factory = getSessionFactory();
    Session session = factory.openSession();
    FullTextSession fullTextSession = Search.getFullTextSession( session );
    fullTextSession.beginTransaction();
    fullTextSession.purgeAll( clazz );
    fullTextSession.getTransaction().commit();
    session.close();
View Full Code Here

  static Map<ProcessEngine, String[]> cleanSqlCache = new HashMap<ProcessEngine, String[]>();
  static Map<ProcessEngine, String[]> tableNamesCache = new HashMap<ProcessEngine, String[]>();
 
  public static void clean(ProcessEngine processEngine) {
    SessionFactory sessionFactory = processEngine.get(SessionFactory.class);
    // when running this with a remote ejb invocation configuration, there is no
    // session factory and no cleanup needs to be done
    if (sessionFactory==null) {
      return;
    }
   
    String[] cleanSql = cleanSqlCache.get(processEngine);

    if (cleanSql == null) {
      Configuration configuration = processEngine.get(Configuration.class);
     
      SessionFactoryImplementor sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory;
      Dialect dialect = sessionFactoryImplementor.getDialect();

      // loop over all foreign key constraints
      List<String> dropForeignKeysSql = new ArrayList<String>();
      List<String> createForeignKeysSql = new ArrayList<String>();
      Iterator<Table> iter = configuration.getTableMappings();
     
      //if no session-factory is build, the configuration is not fully initialized.
      //Hence, the ForeignKey's won't have a referenced table. This is calculated on
      //second pass.
      configuration.buildMappings();
     
     
      while (iter.hasNext()) {
        Table table = (Table) iter.next();
        if (table.isPhysicalTable()) {
          String catalog = table.getCatalog();
          String schema = table.getSchema();
          Iterator<ForeignKey> subIter = table.getForeignKeyIterator();
          while (subIter.hasNext()) {
            ForeignKey fk = (ForeignKey) subIter.next();
            if (fk.isPhysicalConstraint()) {
              // collect the drop foreign key constraint sql
              dropForeignKeysSql.add(fk.sqlDropString(dialect, catalog, schema));
              // MySQLDialect creates an index for each foreign key.
              // see
              // http://opensource.atlassian.com/projects/hibernate/browse/HHH-2155
              // This index should be dropped or an error will be thrown during
              // the creation phase
              if (dialect instanceof MySQLDialect) {
                dropForeignKeysSql.add("alter table " + table.getName() + " drop key " + fk.getName());
              }
              // and collect the create foreign key constraint sql
              createForeignKeysSql.add(fk.sqlCreateString(dialect, sessionFactoryImplementor, catalog, schema));
            }
          }
        }
      }

      List<String> deleteSql = new ArrayList<String>();
      iter = configuration.getTableMappings();
      while (iter.hasNext()) {
        Table table = (Table) iter.next();
        if (table.isPhysicalTable()) {
          deleteSql.add("delete from " + table.getName());
        }
      }

      // glue
      // - drop foreign key constraints
      // - delete contents of all tables
      // - create foreign key constraints
      // together to form the clean script
      List<String> cleanSqlList = new ArrayList<String>();
      cleanSqlList.addAll(dropForeignKeysSql);
      cleanSqlList.addAll(deleteSql);
      cleanSqlList.addAll(createForeignKeysSql);

      cleanSql = (String[]) cleanSqlList.toArray(new String[cleanSqlList.size()]);
     
      cleanSqlCache.put(processEngine, cleanSql);
    }

    Session session = sessionFactory.openSession();
    try {
      for (String query : cleanSql) {
        // log.trace(query);
        session.createSQLQuery(query).executeUpdate();
      }
View Full Code Here

      session.close();
    }
  }

  public static String verifyClean(ProcessEngine processEngine) {
    SessionFactory sessionFactory = processEngine.get(SessionFactory.class);
    // when running this with a remote ejb invocation configuration, there is no
    // session factory and no cleanup needs to be done
    if (sessionFactory==null) {
      return null;
    }
   
    String[] tableNames = tableNamesCache.get(processEngine);

    if (tableNames == null) {
      Configuration configuration = processEngine.get(Configuration.class);
     
      // loop over all foreign key constraints
      List<String> tableNamesList = new ArrayList<String>();
      Iterator iter = configuration.getTableMappings();
      while (iter.hasNext()) {
        Table table = (Table) iter.next();
        if (table.isPhysicalTable()) {
          tableNamesList.add(table.getName());
        }
      }

      tableNames = tableNamesList.toArray(new String[tableNamesList.size()]);
     
      tableNamesCache.put(processEngine, tableNames);
    }

    String recordsLeftMsg = "";
    Session session = sessionFactory.openSession();
    try {
      for (String tableName : tableNames) {
        String countSql = "select count(*) as RECORD_COUNT_ from "+tableName;
        SQLQuery sqlQuery = session.createSQLQuery(countSql);
        sqlQuery.addScalar("RECORD_COUNT_", Hibernate.LONG);
View Full Code Here

   
    return configuration;
  }

  public static SessionFactory buildSessionFactory(Configuration configuration) {
    SessionFactory sessionFactory = null;
    // create the hibernate session factory
    log.debug("building hibernate session factory");
    sessionFactory = configuration.buildSessionFactory();
    return sessionFactory;
  }
View Full Code Here

    return createSessionFactory(configuration, isConfigLookupEnabled);
  }

  public static SessionFactory createSessionFactory(Configuration configuration,
      boolean isConfigLookupEnabled) {
    SessionFactory sessionFactory = configuration.buildSessionFactory();
    if (isConfigLookupEnabled) {
      configurations.put(sessionFactory, configuration);
    }
    return sessionFactory;
  }
View Full Code Here

    LOG.trace( "Resolving serialized SessionFactory" );
    return locateSessionFactoryOnDeserialization( uuid, name );
  }

  private static SessionFactory locateSessionFactoryOnDeserialization(String uuid, String name) throws InvalidObjectException{
    final SessionFactory uuidResult = SessionFactoryRegistry.INSTANCE.getSessionFactory( uuid );
    if ( uuidResult != null ) {
      LOG.debugf( "Resolved SessionFactory by UUID [%s]", uuid );
      return uuidResult;
    }

    // in case we were deserialized in a different JVM, look for an instance with the same name
    // (provided we were given a name)
    if ( name != null ) {
      final SessionFactory namedResult = SessionFactoryRegistry.INSTANCE.getNamedSessionFactory( name );
      if ( namedResult != null ) {
        LOG.debugf( "Resolved SessionFactory by name [%s]", name );
        return namedResult;
      }
    }
View Full Code Here

   * Associates the given session with the current thread of execution.
   *
   * @param session The session to bind.
   */
  public static void bind(org.hibernate.Session session) {
    SessionFactory factory = session.getSessionFactory();
    cleanupAnyOrphanedSession( factory );
    doBind( session, factory );
  }
View Full Code Here

   public SessionFactory buildSessionFactory() throws HibernateException
   {

      ServiceRegistry servReg = new ServiceRegistryBuilder().applySettings(getProperties()).buildServiceRegistry();
      SessionFactory sessionFactory = buildSessionFactory(servReg);

      return sessionFactory;
   }
View Full Code Here

TOP

Related Classes of org.hibernate.SessionFactory

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.