Example usage for org.hibernate.cfg Configuration addClass

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

Introduction

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

Prototype

public Configuration addClass(Class persistentClass) throws MappingException 

Source Link

Document

Read a mapping as an application resource using the convention that a class named foo.bar.Foo is mapped by a file foo/bar/Foo.hbm.xml which can be resolved as a classpath resource.

Usage

From source file:org.jbpm.context.exe.StringIdVariableDbTest.java

License:Open Source License

protected JbpmConfiguration getJbpmConfiguration() {
    if (jbpmConfiguration == null) {
        // disable logging service to prevent logs from referencing custom object
        jbpmConfiguration = JbpmConfiguration.parseResource("org/jbpm/context/exe/jbpm.cfg.xml");

        JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext();
        try {/* ww w  . j a  v a 2 s .  c  om*/
            DbPersistenceServiceFactory persistenceServiceFactory = (DbPersistenceServiceFactory) jbpmContext
                    .getServiceFactory(Services.SERVICENAME_PERSISTENCE);
            Configuration configuration = persistenceServiceFactory.getConfiguration();
            configuration.addClass(CustomStringClass.class);

            JbpmSchema jbpmSchema = new JbpmSchema(configuration);
            jbpmSchema.createTable("JBPM_TEST_CUSTOMSTRINGID");
        } finally {
            jbpmContext.close();
        }
    }
    return jbpmConfiguration;
}

From source file:org.olat.core.commons.persistence.OLATLocalSessionFactoryBean.java

License:Apache License

@Override
protected void postProcessMappings(Configuration config) throws HibernateException {
    try {//  w  w w  .j  ava  2  s . c o  m
        if (additionalDBMappings != null && additionalDBMappings.length > 0) {
            for (AdditionalDBMappings addMapping : additionalDBMappings) {
                List<String> xmlFiles = addMapping.getXmlFiles();
                if (xmlFiles != null) {
                    for (String mapping : xmlFiles) {
                        // we cannot access the classloader magic used by the LocalSessionFactoryBean
                        Resource resource = new ClassPathResource(mapping.trim());
                        config.addInputStream(resource.getInputStream());
                    }
                }

                List<Class<?>> annotatedClasses = addMapping.getAnnotatedClasses();
                if (annotatedClasses != null) {
                    for (Class<?> annotatedClass : annotatedClasses) {
                        config.addClass(annotatedClass);
                    }
                }
            }
        }
        super.postProcessMappings(config);
    } catch (Exception e) {
        log.error("Error during the post processing of the hibernate session factory.", e);
    }
}

From source file:org.openbp.server.persistence.hibernate.HibernatePersistenceContextProvider.java

License:Apache License

private static void addClassMappingToConfiguration(Configuration configuration, Class cls) {
    String className = cls.getName();
    for (Iterator it = configuration.getClassMappings(); it.hasNext();) {
        PersistentClass pc = (PersistentClass) it.next();
        if (pc.getClassName().equals(className)) {
            // Already mapped
            return;
        }//  w w w  .j  a v a  2  s  .  co m
    }
    configuration.addClass(cls);
}

From source file:org.openbravo.base.model.ModelSessionFactoryController.java

License:Open Source License

@Override
protected void mapModel(Configuration cfg) {
    cfg.addClass(Table.class);
    cfg.addClass(Package.class);
    cfg.addClass(Column.class);
    cfg.addClass(Reference.class);
    cfg.addClass(RefSearch.class);
    cfg.addClass(RefTable.class);
    cfg.addClass(RefList.class);
    cfg.addClass(Module.class);
    for (Class<?> clz : additionalClasses) {
        cfg.addClass(clz);/*www . j  a v a  2  s  .  c  om*/
    }
}

From source file:org.opendds.jms.persistence.HibernatePersistenceManager.java

License:Open Source License

public HibernatePersistenceManager(Properties properties) {
    Configuration cfg = new Configuration();

    for (Class persistentClass : getPersistentClasses()) {
        cfg.addClass(persistentClass);
    }/* w w w.j  a va  2  s  .  c  o  m*/
    cfg.setProperties(properties);

    SessionFactory sessionFactory = cfg.buildSessionFactory();

    // Initialize persistence stores
    durableSubscriptionStore = new DurableSubscriptionStore(sessionFactory);
}

From source file:org.openvpms.tools.security.loader.SecurityLoader.java

License:Open Source License

/**
 * Initialise the sesion factory/* w  w w.j av  a 2s  . co  m*/
 */
private void init() throws Exception {
    Configuration config = new Configuration();
    config.addClass(Contact.class);
    config.addClass(Entity.class);
    config.addClass(Act.class);
    config.addClass(ActRelationship.class);
    config.addClass(Participation.class);
    config.addClass(EntityRelationship.class);
    config.addClass(EntityIdentity.class);
    config.addClass(Lookup.class);
    config.addClass(LookupRelationship.class);
    config.addClass(ArchetypeDescriptor.class);
    config.addClass(NodeDescriptor.class);
    config.addClass(AssertionDescriptor.class);
    config.addClass(AssertionTypeDescriptor.class);
    config.addClass(ActionTypeDescriptor.class);
    config.addClass(ProductPrice.class);
    config.addClass(SecurityRole.class);
    config.addClass(ArchetypeAwareGrantedAuthority.class);
    sessionFactory = config.buildSessionFactory();
}

From source file:org.yawlfoundation.yawl.elements.data.external.HibernateEngine.java

License:Open Source License

public void configureSession(Properties props, List<Class> classes) {
    Configuration cfg = new Configuration();
    cfg.setProperties(props);//w  w w  . j ava  2s. c  o m

    if (classes != null) {
        for (Class className : classes) {
            cfg.addClass(className);
        }
    }

    _factory = cfg.buildSessionFactory(); // get a session context        

    // check tables exist and are of a matching format to the persisted objects
    new SchemaUpdate(cfg).execute(false, true);
}

From source file:org.yawlfoundation.yawl.engine.YPersistenceManager.java

License:Open Source License

protected SessionFactory initialise(boolean journalising) throws YPersistenceException {
    Configuration cfg;

    // Create the Hibernate config, check and create database if required,
    // and generally set things up .....
    if (journalising) {
        try {/*from   ww w .  j  ava  2  s . c  o  m*/
            cfg = new Configuration();
            for (Class persistedClass : persistedClasses) {
                cfg.addClass(persistedClass);
            }

            factory = cfg.buildSessionFactory();
            new SchemaUpdate(cfg).execute(false, true);
            setEnabled(true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.fatal("Failure initialising persistence layer", e);
            throw new YPersistenceException("Failure initialising persistence layer", e);
        }
    }
    return factory;
}

From source file:org.yawlfoundation.yawl.util.HibernateEngine.java

License:Open Source License

/** initialises hibernate and the required tables */
private void initialise(Set<Class> classes, Properties props) throws HibernateException {
    try {//from   w ww.ja v a  2s .c om
        Configuration _cfg = new Configuration();

        // if props supplied, use them instead of hibernate.properties
        if (props != null) {
            _cfg.setProperties(props);
        }

        // add each persisted class to config
        for (Class persistedClass : classes) {
            _cfg.addClass(persistedClass);
        }

        // get a session context
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(_cfg.getProperties()).build();
        _factory = _cfg.buildSessionFactory(serviceRegistry);

        // check tables exist and are of a matching format to the persisted objects
        new SchemaUpdate(_cfg).execute(false, true);

    } catch (MappingException me) {
        _log.error("Could not initialise database connection.", me);
    }
}

From source file:storybook.model.hbn.SbSessionFactory.java

License:Open Source License

public void init(String filename) {
    SbApp.trace("SbSessionFactory.init()");
    if (SbApp.getTraceHibernate()) {
        java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.INFO);
    } else {/* www .  j a v  a 2 s  .com*/
        java.util.logging.Logger.getLogger("org.hibernate").setLevel(Level.OFF);
    }
    try {
        // create the SessionFactory from given config file
        // modif favdb remplacement du configFile par la programmation directe
        //System.out.println("filename="+filename);
        //System.out.println("configFile="+configFile);
        Configuration config = new Configuration()/*.configure(configFile)*/;
        config.setProperty("hibernate.show_sql", "false");
        config.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        config.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
        String dbURL = "jdbc:h2:" + filename;
        if (SbApp.getTraceHibernate()) {
            dbURL += ";TRACE_LEVEL_FILE=3;TRACE_LEVEL_SYSTEM_OUT=3";
        } else {
            dbURL += ";TRACE_LEVEL_FILE=0;TRACE_LEVEL_SYSTEM_OUT=0";
        }
        config.setProperty("hibernate.connection.url", dbURL);
        config.setProperty("hibernate.connection.username", "sa");
        config.setProperty("hibernate.connection.password", "");
        config.setProperty("hibernate.hbm2ddl.auto", "update");
        if (SbApp.getTraceHibernate()) {
            java.util.Properties p = new java.util.Properties(System.getProperties());
            p.put("com.mchange.v2.log.MLog", "com.mchange.v2.log.FallbackMLog");
            p.put("com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL", "OFF");
            System.setProperties(p);
        }
        config.setProperty("connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
        config.setProperty("hibernate.c3p0.debug", "0");
        config.setProperty("hibernate.c3p0.min_size", "0");
        config.setProperty("hibernate.c3p0.max_size", "1");
        config.setProperty("hibernate.c3p0.timeout", "5000");
        config.setProperty("hibernate.c3p0.max_statements", "100");
        config.setProperty("hibernate.c3p0.idle_test_period", "300");
        config.setProperty("hibernate.c3p0.acquire_increment", "2");
        config.setProperty("current_session_context_class", "thread");
        config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider");
        config.setProperty("hibernate.current_session_context_class", "thread");
        //if (configFile.contains("preference")) {
        config.addClass(storybook.model.hbn.entity.Preference.class);
        //} else {
        config.addClass(storybook.model.hbn.entity.Part.class);
        config.addClass(storybook.model.hbn.entity.Chapter.class);
        config.addClass(storybook.model.hbn.entity.Scene.class);
        config.addClass(storybook.model.hbn.entity.Gender.class);
        config.addClass(storybook.model.hbn.entity.Person.class);
        config.addClass(storybook.model.hbn.entity.Relationship.class);
        config.addClass(storybook.model.hbn.entity.Location.class);
        config.addClass(storybook.model.hbn.entity.Strand.class);
        config.addClass(storybook.model.hbn.entity.AbstractTag.class);
        config.addClass(storybook.model.hbn.entity.AbstractTagLink.class);
        config.addClass(storybook.model.hbn.entity.Idea.class);
        config.addClass(storybook.model.hbn.entity.Internal.class);
        config.addClass(storybook.model.hbn.entity.Category.class);
        config.addClass(storybook.model.hbn.entity.Attribute.class);
        config.addClass(storybook.model.hbn.entity.TimeEvent.class);
        //}
        sessionFactory = config.buildSessionFactory();
    } catch (SecurityException | HibernateException ex) {
        // make sure you log the exception, as it might be swallowed
        System.err.println("SbSessionFactory.init()");
        System.err.println("*** Initial SessionFactory creation failed: ");
        System.err.println("*** msg: " + ex.getMessage());
        throw new ExceptionInInitializerError(ex);
    }
}