Package org.hibernate.cfg

Examples of org.hibernate.cfg.Configuration$MappingsImpl$TableColumnNameBinding


  private static final Logger log = LoggerFactory.getLogger(SchemaExporter.class);
 
  private Configuration cfg;

  public SchemaExporter(List<String> packageNames) throws ClassNotFoundException {
    cfg = new Configuration();
    cfg.setProperty("hibernate.hbm2ddl.auto", "create");

    for (String packageName : packageNames) {
      for (Class<Object> clazz : getClasses(packageName)) {
        cfg.addAnnotatedClass(clazz);
View Full Code Here


    ( (ClassLoaderServiceImpl) classLoaderService ).withTccl(
        new ClassLoaderServiceImpl.Work() {
          @Override
          public Object perform() {
            final Configuration hibernateConfiguration = buildHibernateConfiguration( serviceRegistry );
            JpaSchemaGenerator.performGeneration( hibernateConfiguration, serviceRegistry );
            return null;
          }
        }
    );
View Full Code Here

  }

  public Configuration buildHibernateConfiguration(ServiceRegistry serviceRegistry) {
    Properties props = new Properties();
    props.putAll( configurationValues );
    Configuration cfg = new Configuration().setProperties( props );

    cfg.setEntityNotFoundDelegate( jpaEntityNotFoundDelegate );

    if ( namingStrategy != null ) {
      cfg.setNamingStrategy( namingStrategy );
    }

    if ( sessionFactoryInterceptor != null ) {
      cfg.setInterceptor( sessionFactoryInterceptor );
    }

    final Object strategyProviderValue = props.get( AvailableSettings.IDENTIFIER_GENERATOR_STRATEGY_PROVIDER );
    final IdentifierGeneratorStrategyProvider strategyProvider = strategyProviderValue == null
        ? null
        : serviceRegistry.getService( StrategySelector.class )
            .resolveStrategy( IdentifierGeneratorStrategyProvider.class, strategyProviderValue );

    if ( strategyProvider != null ) {
      final MutableIdentifierGeneratorFactory identifierGeneratorFactory = cfg.getIdentifierGeneratorFactory();
      for ( Map.Entry<String,Class<?>> entry : strategyProvider.getStrategies().entrySet() ) {
        identifierGeneratorFactory.register( entry.getKey(), entry.getValue() );
      }
    }

    if ( grantedJaccPermissions != null ) {
      final JaccService jaccService = serviceRegistry.getService( JaccService.class );
      for ( GrantedPermission grantedPermission : grantedJaccPermissions ) {
        jaccService.addPermission( grantedPermission );
      }
    }

    if ( cacheRegionDefinitions != null ) {
      for ( CacheRegionDefinition cacheRegionDefinition : cacheRegionDefinitions ) {
        if ( cacheRegionDefinition.cacheType == CacheRegionDefinition.CacheType.ENTITY ) {
          cfg.setCacheConcurrencyStrategy(
              cacheRegionDefinition.role,
              cacheRegionDefinition.usage,
              cacheRegionDefinition.region,
              cacheRegionDefinition.cacheLazy
          );
        }
        else {
          cfg.setCollectionCacheConcurrencyStrategy(
              cacheRegionDefinition.role,
              cacheRegionDefinition.usage,
              cacheRegionDefinition.region
          );
        }
      }
    }


    // todo : need to have this use the metamodel codebase eventually...

    for ( JaxbHibernateConfiguration.JaxbSessionFactory.JaxbMapping jaxbMapping : cfgXmlNamedMappings ) {
      if ( jaxbMapping.getClazz() != null ) {
        cfg.addAnnotatedClass(
            serviceRegistry.getService( ClassLoaderService.class ).classForName( jaxbMapping.getClazz() )
        );
      }
      else if ( jaxbMapping.getResource() != null ) {
        cfg.addResource( jaxbMapping.getResource() );
      }
      else if ( jaxbMapping.getJar() != null ) {
        cfg.addJar( new File( jaxbMapping.getJar() ) );
      }
      else if ( jaxbMapping.getPackage() != null ) {
        cfg.addPackage( jaxbMapping.getPackage() );
      }
    }

    List<Class> loadedAnnotatedClasses = (List<Class>) configurationValues.remove( AvailableSettings.LOADED_CLASSES );
    if ( loadedAnnotatedClasses != null ) {
      for ( Class cls : loadedAnnotatedClasses ) {
        cfg.addAnnotatedClass( cls );
      }
    }

    for ( String className : metadataSources.getAnnotatedMappingClassNames() ) {
      cfg.addAnnotatedClass( serviceRegistry.getService( ClassLoaderService.class ).classForName( className ) );
    }

    for ( MetadataSources.ConverterDescriptor converterDescriptor : metadataSources.getConverterDescriptors() ) {
      final Class<? extends AttributeConverter> converterClass;
      try {
        Class theClass = serviceRegistry.getService( ClassLoaderService.class ).classForName( converterDescriptor.converterClassName );
        converterClass = (Class<? extends AttributeConverter>) theClass;
      }
      catch (ClassCastException e) {
        throw persistenceException(
            String.format(
                "AttributeConverter implementation [%s] does not implement AttributeConverter interface",
                converterDescriptor.converterClassName
            )
        );
      }
      cfg.addAttributeConverter( converterClass, converterDescriptor.autoApply );
    }

    for ( String resourceName : metadataSources.mappingFileResources ) {
      Boolean useMetaInf = null;
      try {
        if ( resourceName.endsWith( META_INF_ORM_XML ) ) {
          useMetaInf = true;
        }
        cfg.addResource( resourceName );
      }
      catch( MappingNotFoundException e ) {
        if ( ! resourceName.endsWith( META_INF_ORM_XML ) ) {
          throw persistenceException( "Unable to find XML mapping file in classpath: " + resourceName );
        }
        else {
          useMetaInf = false;
          //swallow it, the META-INF/orm.xml is optional
        }
      }
      catch( MappingException me ) {
        throw persistenceException( "Error while reading JPA XML file: " + resourceName, me );
      }

      if ( Boolean.TRUE.equals( useMetaInf ) ) {
        LOG.exceptionHeaderFound( getExceptionHeader(), META_INF_ORM_XML );
      }
      else if (Boolean.FALSE.equals(useMetaInf)) {
        LOG.exceptionHeaderNotFound( getExceptionHeader(), META_INF_ORM_XML );
      }
    }
    for ( NamedInputStream namedInputStream : metadataSources.namedMappingFileInputStreams ) {
      try {
        //addInputStream has the responsibility to close the stream
        cfg.addInputStream( new BufferedInputStream( namedInputStream.getStream() ) );
      }
      catch ( InvalidMappingException e ) {
        // try our best to give the file name
        if ( StringHelper.isNotEmpty( namedInputStream.getName() ) ) {
          throw new InvalidMappingException(
              "Error while parsing file: " + namedInputStream.getName(),
              e.getType(),
              e.getPath(),
              e
          );
        }
        else {
          throw e;
        }
      }
      catch (MappingException me) {
        // try our best to give the file name
        if ( StringHelper.isNotEmpty( namedInputStream.getName() ) ) {
          throw new MappingException("Error while parsing file: " + namedInputStream.getName(), me );
        }
        else {
          throw me;
        }
      }
    }
    for ( String packageName : metadataSources.packageNames ) {
      cfg.addPackage( packageName );
    }
   
    final TypeContributorList typeContributorList
        = (TypeContributorList) configurationValues.get( TYPE_CONTRIBUTORS );
    if ( typeContributorList != null ) {
      configurationValues.remove( TYPE_CONTRIBUTORS );
      for ( TypeContributor typeContributor : typeContributorList.getTypeContributors() ) {
        cfg.registerTypeContributor( typeContributor );
      }
    }
   
    return cfg;
  }
View Full Code Here

    if ( getSessions() != null ) getSessions().close();

    try {

      setCfg( new Configuration() );

      cfg.addProperties( getExtraProperties() );

      if ( recreateSchema() ) {
        cfg.setProperty( Environment.HBM2DDL_AUTO, "create-drop" );
View Full Code Here

    return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).build();
  }

  public static void main(String[] args) {
    try {
      Configuration cfg = new Configuration();

      String propFile = null;

      for ( int i = 0; i < args.length; i++ ) {
        if ( args[i].startsWith( "--" ) ) {
          if ( args[i].startsWith( "--properties=" ) ) {
            propFile = args[i].substring( 13 );
          }
          else if ( args[i].startsWith( "--config=" ) ) {
            cfg.configure( args[i].substring( 9 ) );
          }
          else if ( args[i].startsWith( "--naming=" ) ) {
            cfg.setNamingStrategy(
                ( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance()
            );
          }
        }
        else {
          cfg.addFile( args[i] );
        }

      }

      if ( propFile != null ) {
        Properties props = new Properties();
        props.putAll( cfg.getProperties() );
        props.load( new FileInputStream( propFile ) );
        cfg.setProperties( props );
      }

      StandardServiceRegistryImpl serviceRegistry = createServiceRegistry( cfg.getProperties() );
      try {
        new SchemaValidator( serviceRegistry, cfg ).validate();
      }
      finally {
        serviceRegistry.destroy();
View Full Code Here

   * Execute the task
   */
  @Override
    public void execute() throws BuildException {
    try {
      Configuration cfg = getConfiguration();
      getSchemaValidator(cfg).validate();
    }
    catch (HibernateException e) {
      throw new BuildException("Schema text failed: " + e.getMessage(), e);
    }
View Full Code Here

    return ArrayHelper.toStringArray( files );
  }

  private Configuration getConfiguration() throws Exception {
    Configuration cfg = new Configuration();
    if (namingStrategy!=null) {
      cfg.setNamingStrategy(
          (NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance()
        );
    }
    if (configurationFile!=null) {
      cfg.configure( configurationFile );
    }

    String[] files = getFiles();
    for (int i = 0; i < files.length; i++) {
      String filename = files[i];
      if ( filename.endsWith(".jar") ) {
        cfg.addJar( new File(filename) );
      }
      else {
        cfg.addFile(filename);
      }
    }
    return cfg;
  }
View Full Code Here

    return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).build();
  }

  public static void main(String[] args) {
    try {
      Configuration cfg = new Configuration();

      boolean script = true;
      boolean drop = false;
      boolean create = false;
      boolean halt = false;
      boolean export = true;
      String outFile = null;
      String importFile = DEFAULT_IMPORT_FILE;
      String propFile = null;
      boolean format = false;
      String delim = null;

      for ( int i = 0; i < args.length; i++ ) {
        if ( args[i].startsWith( "--" ) ) {
          if ( args[i].equals( "--quiet" ) ) {
            script = false;
          }
          else if ( args[i].equals( "--drop" ) ) {
            drop = true;
          }
          else if ( args[i].equals( "--create" ) ) {
            create = true;
          }
          else if ( args[i].equals( "--haltonerror" ) ) {
            halt = true;
          }
          else if ( args[i].equals( "--text" ) ) {
            export = false;
          }
          else if ( args[i].startsWith( "--output=" ) ) {
            outFile = args[i].substring( 9 );
          }
          else if ( args[i].startsWith( "--import=" ) ) {
            importFile = args[i].substring( 9 );
          }
          else if ( args[i].startsWith( "--properties=" ) ) {
            propFile = args[i].substring( 13 );
          }
          else if ( args[i].equals( "--format" ) ) {
            format = true;
          }
          else if ( args[i].startsWith( "--delimiter=" ) ) {
            delim = args[i].substring( 12 );
          }
          else if ( args[i].startsWith( "--config=" ) ) {
            cfg.configure( args[i].substring( 9 ) );
          }
          else if ( args[i].startsWith( "--naming=" ) ) {
            cfg.setNamingStrategy(
                ( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) )
                    .newInstance()
            );
          }
        }
        else {
          String filename = args[i];
          if ( filename.endsWith( ".jar" ) ) {
            cfg.addJar( new File( filename ) );
          }
          else {
            cfg.addFile( filename );
          }
        }

      }

      if ( propFile != null ) {
        Properties props = new Properties();
        props.putAll( cfg.getProperties() );
        props.load( new FileInputStream( propFile ) );
        cfg.setProperties( props );
      }

      if (importFile != null) {
        cfg.setProperty( AvailableSettings.HBM2DDL_IMPORT_FILES, importFile );
      }

      StandardServiceRegistryImpl serviceRegistry = createServiceRegistry( cfg.getProperties() );
      try {
        SchemaExport se = new SchemaExport( serviceRegistry, cfg )
            .setHaltOnError( halt )
            .setOutputFile( outFile )
            .setDelimiter( delim )
View Full Code Here

  @Override
    public void execute() throws BuildException {
    try {
      log("Running Hibernate Core SchemaUpdate.");
      log("This is an Ant task supporting only mapping files, if you want to use annotations see http://tools.hibernate.org.");
      Configuration cfg = getConfiguration();
      getSchemaUpdate(cfg).execute(!quiet, !text);
    }
    catch (HibernateException e) {
      throw new BuildException("Schema text failed: " + e.getMessage(), e);
    }
View Full Code Here

    return ArrayHelper.toStringArray( files );
  }

  private Configuration getConfiguration() throws Exception {
    Configuration cfg = new Configuration();
    if (namingStrategy!=null) {
      cfg.setNamingStrategy(
          (NamingStrategy) ReflectHelper.classForName( namingStrategy ).newInstance()
        );
    }
    if (configurationFile!=null) {
      cfg.configure( configurationFile );
    }

    String[] files = getFiles();
    for (int i = 0; i < files.length; i++) {
      String filename = files[i];
      if ( filename.endsWith(".jar") ) {
        cfg.addJar( new File(filename) );
      }
      else {
        cfg.addFile(filename);
      }
    }
    return cfg;
  }
View Full Code Here

TOP

Related Classes of org.hibernate.cfg.Configuration$MappingsImpl$TableColumnNameBinding

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.