Example usage for org.hibernate.cfg Configuration addResource

List of usage examples for org.hibernate.cfg Configuration addResource

Introduction

In this page you can find the example usage for org.hibernate.cfg Configuration addResource.

Prototype

public Configuration addResource(String resourceName) throws MappingException 

Source Link

Document

Read mappings as a application resourceName (i.e.

Usage

From source file:org.jpos.ee.support.JPosHibernateConfiguration.java

License:Open Source License

private void parseMapping(Configuration cfg, Element mapping, String moduleName) throws ConfigurationException {
    final String resource = mapping.attributeValue("resource");
    final String clazz = mapping.attributeValue("class");

    if (resource != null) {
        cfg.addResource(resource);
    } else if (clazz != null) {
        try {//from  w  w  w . j  a  v a  2  s .  com
            cfg.addAnnotatedClass(ReflectHelper.classForName(clazz));
        } catch (ClassNotFoundException e) {
            throw new ConfigurationException(
                    "Class " + clazz + " specified in mapping for module " + moduleName + " cannot be found");
        }
    } else {
        throw new ConfigurationException(
                "<mapping> element in configuration specifies no known attributes at module " + moduleName);
    }
}

From source file:org.n52.sos.ds.hibernate.SessionFactoryProvider.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*  www. j  ava2 s .  c  o m*/
protected Configuration getConfiguration(Properties properties) throws ConfigurationException {
    try {
        Configuration configuration = new Configuration().configure("/sos-hibernate.cfg.xml");
        if (properties.containsKey(HIBERNATE_RESOURCES)) {
            List<String> resources = (List<String>) properties.get(HIBERNATE_RESOURCES);
            for (String resource : resources) {
                configuration.addResource(resource);
            }
            properties.remove(HIBERNATE_RESOURCES);
        } else if (properties.containsKey(HIBERNATE_DIRECTORY)) {
            String directories = (String) properties.get(HIBERNATE_DIRECTORY);
            for (String directory : directories.split(PATH_SEPERATOR)) {
                File hibernateDir = new File(directory);
                if (!hibernateDir.exists()) {
                    // try to configure from classpath (relative path)
                    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                    URL dirUrl = classLoader.getResource(directory);
                    if (dirUrl != null) {
                        try {
                            hibernateDir = new File(
                                    URLDecoder.decode(dirUrl.getPath(), Charset.defaultCharset().toString()));
                        } catch (UnsupportedEncodingException e) {
                            throw new ConfigurationException("Unable to encode directory URL " + dirUrl + "!");
                        }
                    }
                }
                if (!hibernateDir.exists()) {
                    throw new ConfigurationException("Hibernate directory " + directory + " doesn't exist!");
                }
                configuration.addDirectory(hibernateDir);
            }
        } else {
            // keep this as default/fallback
            configuration.addDirectory(new File(getClass().getResource(HIBERNATE_MAPPING_CORE_PATH).toURI()));
            configuration.addDirectory(
                    new File(getClass().getResource(HIBERNATE_MAPPING_TRANSACTIONAL_PATH).toURI()));
            configuration.addDirectory(new File(
                    getClass().getResource(HIBERNATE_MAPPING_SERIES_CONCEPT_OBSERVATION_PATH).toURI()));
        }
        return configuration;
    } catch (HibernateException he) {
        String exceptionText = "An error occurs during instantiation of the database connection pool!";
        LOGGER.error(exceptionText, he);
        cleanup();
        throw new ConfigurationException(exceptionText, he);
    } catch (URISyntaxException urise) {
        String exceptionText = "An error occurs during instantiation of the database connection pool!";
        LOGGER.error(exceptionText, urise);
        cleanup();
        throw new ConfigurationException(exceptionText, urise);
    }
}

From source file:org.onebusaway.admin.service.bundle.task.GtfsArchiveTask.java

License:Apache License

private Configuration getConfiguration() {
    try {//ww w  . j av a2s. c om

        Context initialContext = new InitialContext();
        Context environmentContext = (Context) initialContext.lookup("java:comp/env");
        DataSource ds = (DataSource) environmentContext.lookup("jdbc/archiveDB");
        if (ds == null) {
            _log.error("unable to locate expected datasource jdbc/archiveDB");
            return null;
        }
        Configuration config = new Configuration();
        config.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/archiveDB");
        config.setProperty("hibernate.connection.pool_size", "1");
        config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
        config.setProperty("hibernate.hbm2ddl.auto", "update");
        config.addResource("org/onebusaway/gtfs/model/GtfsArchiveMapping.hibernate.xml");
        config.addResource("org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.hibernate.xml");

        //performance tuning
        config.setProperty("hibernate.jdbc.batch_size", "4000");
        config.setProperty("hibernate.jdbc.fetch_size", "64");
        config.setProperty("hibernate.order_inserts", "true");
        config.setProperty("hibernate.order_updates", "true");
        config.setProperty("hibernate.cache.use_second_level_cache", "false");
        config.setProperty("hibernate.generate_statistics", "false");
        config.setProperty("hibernate.cache.use_query_cache", "false");

        return config;
    } catch (Throwable t) {
        _log.error("configuration exception:", t);
        return null;
    }
}

From source file:org.onebusaway.gtfs.GtfsDatabaseLoaderMain.java

License:Apache License

private void run(String[] args) throws IOException {
    CommandLine cli = parseCommandLineOptions(args);

    args = cli.getArgs();/*w  w w. j  av  a  2 s  .  c  om*/
    if (args.length != 1) {
        printUsage();
        System.exit(-1);
    }

    Configuration config = new Configuration();
    config.setProperty("hibernate.connection.driver_class", cli.getOptionValue(ARG_DRIVER_CLASS));
    config.setProperty("hibernate.connection.url", cli.getOptionValue(ARG_URL));
    if (cli.hasOption(ARG_USERNAME)) {
        config.setProperty("hibernate.connection.username", cli.getOptionValue(ARG_USERNAME));
    }
    if (cli.hasOption(ARG_PASSWORD)) {
        config.setProperty("hibernate.connection.password", cli.getOptionValue(ARG_PASSWORD));
    }
    config.setProperty("hibernate.connection.pool_size", "1");
    config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
    config.setProperty("hibernate.hbm2ddl.auto", "update");
    config.addResource("org/onebusaway/gtfs/model/GtfsMapping.hibernate.xml");
    config.addResource("org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.hibernate.xml");

    SessionFactory sessionFactory = config.buildSessionFactory();
    HibernateGtfsFactory factory = new HibernateGtfsFactory(sessionFactory);

    GtfsReader reader = new GtfsReader();
    reader.setInputLocation(new File(args[0]));

    GtfsMutableRelationalDao dao = factory.getDao();
    reader.setEntityStore(dao);
    reader.run();
    reader.close();
}

From source file:org.picketlink.idm.impl.store.hibernate.HibernateIdentityStoreImpl.java

License:Open Source License

protected SessionFactory bootstrapHibernateSessionFactory(
        IdentityStoreConfigurationContext configurationContext) throws IdentityException {

    String sfJNDIName = configurationContext.getStoreConfigurationMetaData()
            .getOptionSingleValue(HIBERNATE_SESSION_FACTORY_JNDI_NAME);
    String sfRegistryName = configurationContext.getStoreConfigurationMetaData()
            .getOptionSingleValue(HIBERNATE_SESSION_FACTORY_REGISTRY_NAME);
    String addMappedClasses = configurationContext.getStoreConfigurationMetaData()
            .getOptionSingleValue(ADD_HIBERNATE_MAPPINGS);
    String hibernateConfiguration = configurationContext.getStoreConfigurationMetaData()
            .getOptionSingleValue(HIBERNATE_CONFIGURATION);

    if (sfJNDIName != null) {
        try {/*from  w  ww .  j av a 2s.co m*/
            return (SessionFactory) new InitialContext().lookup(sfJNDIName);
        } catch (NamingException e) {
            if (log.isLoggable(Level.FINER)) {
                log.log(Level.FINER, "Exception occurred: ", e);
            }

            throw new IdentityException(
                    "Cannot obtain hibernate SessionFactory from provided JNDI name: " + sfJNDIName, e);
        }
    } else if (sfRegistryName != null) {
        Object registryObject = configurationContext.getConfigurationRegistry().getObject(sfRegistryName);

        if (registryObject == null) {
            throw new IdentityException(
                    "Cannot obtain hibernate SessionFactory from provided registry name: " + sfRegistryName);
        }

        if (!(registryObject instanceof SessionFactory)) {
            throw new IdentityException("Cannot obtain hibernate SessionFactory from provided registry name: "
                    + sfRegistryName + "; Registered object is not an instance of SessionFactory: "
                    + registryObject.getClass().getName());
        }

        return (SessionFactory) registryObject;

    } else if (hibernateConfiguration != null) {

        try {
            Configuration config = new Configuration().configure(hibernateConfiguration);

            if (addMappedClasses != null && addMappedClasses.equals("false")) {
                return config.buildSessionFactory();
            } else {

                return config.addResource("mappings/HibernateIdentityObject.hbm.xml")
                        .addResource("mappings/HibernateIdentityObjectCredentialBinaryValue.hbm.xml")
                        .addResource("mappings/HibernateIdentityObjectAttributeBinaryValue.hbm.xml")
                        .addResource("mappings/HibernateIdentityObjectAttribute.hbm.xml")
                        .addResource("mappings/HibernateIdentityObjectCredential.hbm.xml")
                        .addResource("mappings/HibernateIdentityObjectCredentialType.hbm.xml")
                        .addResource("mappings/HibernateIdentityObjectRelationship.hbm.xml")
                        .addResource("mappings/HibernateIdentityObjectRelationshipName.hbm.xml")
                        .addResource("mappings/HibernateIdentityObjectRelationshipType.hbm.xml")
                        .addResource("mappings/HibernateIdentityObjectType.hbm.xml")
                        .addResource("mappings/HibernateRealm.hbm.xml").buildSessionFactory();
            }
        } catch (Exception e) {
            if (log.isLoggable(Level.FINER)) {
                log.log(Level.FINER, "Exception occurred: ", e);
            }

            throw new IdentityException(
                    "Cannot obtain hibernate SessionFactory using provided hibernate configuration: "
                            + hibernateConfiguration,
                    e);
        }

    }
    throw new IdentityException("Cannot obtain hibernate SessionFactory. None of supported options specified: "
            + HIBERNATE_SESSION_FACTORY_JNDI_NAME + ", " + HIBERNATE_SESSION_FACTORY_REGISTRY_NAME + ", "
            + HIBERNATE_CONFIGURATION);

}

From source file:org.wso2.mercury.persistence.hibernate.HibernatePersistenceManager.java

License:Apache License

public HibernatePersistenceManager(AxisConfiguration axisConfiguration) {

    Configuration configuration = new Configuration();
    setProperty(configuration, HIBERNATE_CONNECTION_DRIVER_CLASS, axisConfiguration);
    setProperty(configuration, HIBERNATE_CONNECTION_URL, axisConfiguration);
    setProperty(configuration, HIBERNATE_CONNECTION_USERNAME, axisConfiguration);
    setProperty(configuration, HIBERNATE_CONNECTION_PASSWORD, axisConfiguration);
    setProperty(configuration, HIBERNATE_CONNECTION_POOL_SIZE, axisConfiguration);
    setProperty(configuration, HIBERNATE_CURRENT_SESSION_CONTEXT_CLASS, axisConfiguration);
    setProperty(configuration, HIBERNATE_DIALECT, axisConfiguration);
    //        configuration.setProperty("hibernate.show_sql","true");
    //        configuration.setProperty("hibernate.hbm2ddl.auto","create");
    configuration.addResource("org/wso2/mercury/persistence/hibernate/rm.hbm.xml");

    this.sessionFactory = configuration.buildSessionFactory();
}

From source file:org.wso2.mercury.persistence.hibernate.HibernatePersistenceManagerTest.java

License:Apache License

private Configuration getConfiguration() {

    Configuration configuration = new Configuration();
    configuration.setProperty("hibernate.connection.driver_class", "org.apache.derby.jdbc.EmbeddedDriver");
    configuration.setProperty("hibernate.connection.url", URL_STRING);
    configuration.setProperty("hibernate.connection.username", "mercury");
    configuration.setProperty("hibernate.connection.password", "mercury");
    configuration.setProperty("hibernate.connection.pool_size", "1");
    configuration.setProperty("hibernate.current_session_context_class", "thread");
    configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");

    //        configuration.setProperty("hibernate.show_sql","true");
    configuration.setProperty("hibernate.hbm2ddl.auto", "create");
    configuration.addResource("org/wso2/mercury/persistence/hibernate/rm.hbm.xml");

    return configuration;
}

From source file:powerhibernate.HibernateUtil.java

public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
        Configuration configuration = new Configuration().configure();
        ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
                .build();/*from  w w  w.  j  a  v a  2 s .c  o  m*/
        //configuration.addClass(powerhibernate.Student.class);
        // Read mappings as a application resourceName
        // addResource is for add hbml.xml files in case of declarative approach
        configuration.addResource("powerhibernate/Student.hbm.xml");
        sessionFactory = configuration.buildSessionFactory(sr);
    }
    return sessionFactory;
}