Example usage for org.hibernate.cfg Configuration getProperties

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

Introduction

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

Prototype

public Properties getProperties() 

Source Link

Document

Get all properties

Usage

From source file:org.florin.hibernate.HibernateUtil.java

public static SessionFactory getSessionFactory() {
    if (sessionFactory == null) {
        // loads configuration and mappings
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();

        // builds a session factory from the service registry
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    }//from w  w w . j  av a 2  s.  co m

    return sessionFactory;
}

From source file:org.flowerplatform.web.tests.security.sandbox.SecurityPermissionsTests.java

License:Open Source License

@BeforeClass
public static void setUp() {
    EclipseDependentTestSuiteBase.setUp();

    Configuration config = new Configuration();
    Properties props = config.getProperties();
    props.setProperty(Environment.DRIVER, "org.h2.Driver");
    props.setProperty(Environment.USER, "sa");
    props.setProperty(Environment.URL, "jdbc:h2:mem:temp_flower_web;MVCC=TRUE");
    props.setProperty(Environment.PASS, "");
    props.setProperty(Environment.DIALECT, H2Dialect.class.getName());
    props.setProperty(Environment.HBM2DDL_AUTO, "create");
    props.setProperty(Environment.SHOW_SQL, "true");

    props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_CONTAINMENT, "REFRESH,PERSIST,MERGE");
    props.setProperty(PersistenceOptions.PERSISTENCE_XML, "annotations.xml");
    props.setProperty(PersistenceOptions.JOIN_TABLE_FOR_NON_CONTAINED_ASSOCIATIONS, "false");
    props.setProperty(PersistenceOptions.ALWAYS_VERSION, "false");
    //      props.setProperty(PersistenceOptions.INHERITANCE_MAPPING, "TABLE_PER_CLASS");
    props.setProperty(PersistenceOptions.INHERITANCE_MAPPING, "SINGLE_TABLE");
    props.setProperty(PersistenceOptions.ADD_INDEX_FOR_FOREIGN_KEY, "false");

    //      props.setProperty(Environment.DRIVER, "org.postgresql.Driver");
    //      props.setProperty(Environment.USER, "postgres");
    //      props.setProperty(Environment.URL, "jdbc:postgresql://localhost/flower-dev-center");
    //      props.setProperty(Environment.PASS, "postgres");
    //      props.setProperty(Environment.DIALECT, PostgresPlusDialect.class.getName());
    //      props.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
    //      props.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    //      props.setProperty(Environment.SHOW_SQL, "true");

    // the name of the session factory
    String hbName = "Entity";
    // create the HbDataStore using the name
    final HbDataStore hbds = HbHelper.INSTANCE.createRegisterDataStore(hbName);

    // set the properties
    hbds.setDataStoreProperties(props);/*w  ww.  j  a v  a 2s  .  c om*/
    // sets its epackages stored in this datastore
    hbds.setEPackages(new EPackage[] { EntityPackage.eINSTANCE });
    ((HbSessionDataStore) hbds).setConfiguration(config);

    try {
        // initialize
        hbds.initialize();
    } catch (Exception e) {
        System.out.println(e);
    }

    WebPlugin.getInstance().getDatabaseManager().setFactory(hbds.getSessionFactory());
    EventListenerRegistry registry = ((SessionFactoryImpl) hbds.getSessionFactory()).getServiceRegistry()
            .getService(EventListenerRegistry.class);
    SecurityEntityListener listener = new SecurityEntityListener();
    registry.appendListeners(EventType.POST_INSERT, listener);
    registry.appendListeners(EventType.POST_DELETE, listener);
    registry.appendListeners(EventType.POST_UPDATE, listener);
    registry.appendListeners(EventType.PRE_DELETE, listener);

    Utils.deleteAllData();

    System.setProperty("java.security.policy",
            new File(TestUtil.getResourcesDir(SecurityPermissionsTests.class) + "all.policy")
                    .getAbsolutePath());
    PolicyFile policyFile = new PolicyFile();
    Policy.setPolicy(new FlowerWebPolicyTest(policyFile));
    Policy.getPolicy().refresh();
    System.setSecurityManager(new SecurityManager());

    // disable sending mails during testing
    CommonPlugin.getInstance().getFlowerProperties().remove("mail.smtp.host");
    SendMailService service = (SendMailService) CommunicationPlugin.getInstance().getServiceRegistry()
            .getService(SendMailService.SERVICE_ID);
    service.initializeProperties();
}

From source file:org.freaksparty.ficonbar.util.DBSession.java

public static Session getSession() {
    Configuration configuration = new Configuration();
    configuration.configure("hibernate.cfg.xml");
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties());
    SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build());
    Session session = sessionFactory.openSession();
    return session;
}

From source file:org.glite.security.voms.admin.persistence.deployer.SchemaDeployer.java

License:Apache License

private Configuration loadHibernateConfiguration() {

    Configuration cfg;

    if (hibernatePropertiesFile == null) {

        cfg = DBUtil.loadHibernateConfiguration(getVOConfigurationDir(), vo);

    } else {//from w w w .jav a 2 s .c o m

        cfg = DBUtil.loadHibernateConfiguration(hibernatePropertiesFile);
    }

    dialect = Dialect.getDialect(cfg.getProperties());

    cfg.configure();

    return cfg;
}

From source file:org.gluewine.persistence_jpa_hibernate.impl.SessionAspectProvider.java

License:Apache License

@Override
public void codeSourceAdded(List<CodeSource> sources) {
    if (!hasEntities(sources))
        return;//from   w  ww  . j  ava2s. c  o m

    synchronized (factoryLocker) {
        try {
            Configuration config = new Configuration();
            config.setProperties(properties);
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
                    .buildServiceRegistry();

            for (CodeSource source : sources) {
                logger.debug("Processing CodesSource: " + source.getDisplayName());

                for (String entity : source.getEntities()) {
                    Class<?> clazz = source.getSourceClassLoader().loadClass(entity);
                    entities.add(clazz);
                    logger.debug("Adding Hibernate Entity: " + entity);
                }

                if (source.loadSQL() && source instanceof JarCodeSource) {
                    JarInputStream jar = null;
                    BufferedReader reader = null;
                    try {
                        jar = new JarInputStream(((JarCodeSource) source).getURLs()[0].openStream());
                        JarEntry entry = jar.getNextJarEntry();
                        while (entry != null) {
                            String name = entry.getName().toLowerCase(Locale.getDefault());
                            if (name.endsWith(".sql")) {
                                logger.debug("Checking SQL File : " + name);
                                List<String> content = new ArrayList<String>();
                                reader = new BufferedReader(new InputStreamReader(jar, "UTF-8"));
                                String line;
                                while ((line = reader.readLine()) != null)
                                    content.add(line);

                                List<SQLStatement> stmts = new ArrayList<SQLStatement>();

                                if (statements.containsKey(source)) {
                                    Map<String, List<SQLStatement>> m = statements.get(source);
                                    m.put(name, stmts);
                                } else {
                                    Map<String, List<SQLStatement>> m = new TreeMap<String, List<SQLStatement>>();
                                    statements.put(source, m);
                                    m.put(name, stmts);
                                }

                                updateSQLStatements(content, stmts);
                            }
                            entry = jar.getNextJarEntry();
                        }
                    } finally {
                        try {
                            if (jar != null)
                                jar.close();
                        } finally {
                            if (reader != null)
                                reader.close();
                        }
                    }
                }
            }

            for (Class<?> cl : entities)
                config.addAnnotatedClass(cl);

            configuration = config;
            factory = config.buildSessionFactory(serviceRegistry);
            checkStatements();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

From source file:org.gluewine.persistence_jpa_hibernate.impl.SessionAspectProvider.java

License:Apache License

@Override
public void codeSourceRemoved(List<CodeSource> sources) {
    if (!hasEntities(sources))
        return;/*from  w  ww  . j a v  a  2s .  c  o m*/

    synchronized (factoryLocker) {
        try {
            Configuration config = new Configuration();

            ClassLoader loader = config.getClass().getClassLoader();
            GluewineLoader gw = null;
            if (loader instanceof GluewineLoader)
                gw = (GluewineLoader) loader;

            config.setProperties(properties);
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties())
                    .buildServiceRegistry();

            for (CodeSource source : sources) {
                Iterator<Class<?>> iter = entities.iterator();
                while (iter.hasNext()) {
                    if (iter.next().getClassLoader() == source.getSourceClassLoader())
                        iter.remove();
                }

                if (gw != null)
                    gw.removeReference(source.getSourceClassLoader());
            }

            for (Class<?> cl : entities)
                config.addAnnotatedClass(cl);

            configuration = config;
            factory = config.buildSessionFactory(serviceRegistry);
            checkStatements();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

From source file:org.granite.test.tide.hibernate4.data.TestHibernate4ChangeSetMerge.java

License:Open Source License

@Override
protected void initPersistence() {
    Configuration configuration = new Configuration().addAnnotatedClass(AbstractEntity.class)
            .addAnnotatedClass(Address.class).addAnnotatedClass(Contact1.class).addAnnotatedClass(Country.class)
            .addAnnotatedClass(Person1.class).addAnnotatedClass(Phone.class).addAnnotatedClass(Contact2.class)
            .addAnnotatedClass(Person2.class).addAnnotatedClass(Phone2.class)
            .setProperty("hibernate.dialect", org.hibernate.dialect.H2Dialect.class.getName())
            .setProperty("hibernate.hbm2ddl.auto", "create-drop").setProperty("hibernate.show_sql", "true")
            .setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName())
            .setProperty("hibernate.connection.url", "jdbc:h2:mem:test-loader")
            .setProperty("hibernate.connection.username", "sa")
            .setProperty("hibernate.connection.password", "");

    BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
            .with(new Hibernate4ChangeSetIntegrator());
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsrb.build())
            .applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = ssrb.build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}

From source file:org.granite.test.tide.hibernate4.data.TestHibernate4ChangeSetPublisher.java

License:Open Source License

protected void initPersistence() {
    Configuration configuration = new Configuration().addAnnotatedClass(AbstractEntity.class)
            .addAnnotatedClass(LegalEntity.class).addAnnotatedClass(Address.class)
            .addAnnotatedClass(Contact1.class).addAnnotatedClass(Country.class).addAnnotatedClass(Person1.class)
            .addAnnotatedClass(Person4.class).addAnnotatedClass(Contact4.class).addAnnotatedClass(Phone.class)
            .addAnnotatedClass(Phone2.class).addAnnotatedClass(Phone4.class).addAnnotatedClass(OrderRepo.class)
            .addAnnotatedClass(Order.class).addAnnotatedClass(LineItemList.class)
            .addAnnotatedClass(LineItemBag.class).addAnnotatedClass(Order2.class)
            .addAnnotatedClass(LineItemList2.class).addAnnotatedClass(LineItemBag2.class)
            .addAnnotatedClass(Classification.class)
            .setProperty("hibernate.dialect", org.hibernate.dialect.H2Dialect.class.getName())
            .setProperty("hibernate.hbm2ddl.auto", "create-drop").setProperty("hibernate.show_sql", "true")
            .setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName())
            .setProperty("hibernate.connection.url", "jdbc:h2:mem:test-changeset")
            .setProperty("hibernate.connection.username", "sa")
            .setProperty("hibernate.connection.password", "");

    BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
            .with(new Hibernate4ChangeSetIntegrator());
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsrb.build())
            .applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = ssrb.build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}

From source file:org.granite.test.tide.hibernate4.data.TestHibernate4DataPublish.java

License:Open Source License

protected void initPersistence() throws Exception {
    Configuration configuration = new Configuration().addAnnotatedClass(AbstractEntity0.class)
            .addAnnotatedClass(Order3.class).addAnnotatedClass(LineItem.class).addAnnotatedClass(Contact5.class)
            .addAnnotatedClass(Location5.class).addAnnotatedClass(Alias5.class)
            .setProperty("hibernate.dialect", org.hibernate.dialect.H2Dialect.class.getName())
            .setProperty("hibernate.hbm2ddl.auto", "create-drop").setProperty("hibernate.show_sql", "true")
            .setProperty("hibernate.connection.driver_class", "org.h2.Driver")
            .setProperty("hibernate.connection.url", "jdbc:h2:mem:test-publish")
            .setProperty("hibernate.connection.username", "sa")
            .setProperty("hibernate.connection.password", "");

    BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder()
            .with(new Hibernate4Integrator());
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsrb.build())
            .applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = ssrb.build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}

From source file:org.halyph.sessiondemo.Demo.java

License:Open Source License

public static void main(String[] args) {
    // configures settings from hibernate.cfg.xml
    // Hibernate 3 style
    //      SessionFactory sessionFactory = new Configuration().configure()
    //            .buildSessionFactory();

    // Hibernate 4 style
    Configuration configuration = new Configuration();
    configuration.configure();/*from   w  ww .  jav  a2s  . c  o m*/
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
            .buildServiceRegistry();
    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);

    // ------------------------------

    // create a couple of events...
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(new Event("Our very first event!", new Date()));
    session.save(new Event("A follow up event", new Date()));
    session.getTransaction().commit();
    session.close();

    // now lets pull events from the database and list them
    session = sessionFactory.openSession();
    session.beginTransaction();
    List result = session.createQuery("from Event").list();
    for (Event event : (List<Event>) result) {
        System.out.println("Event (" + event.getDate() + ") : " + event.getTitle());
    }
    session.getTransaction().commit();
    session.close();

    if (sessionFactory != null) {
        sessionFactory.close();
    }
}