Example usage for org.hibernate.boot MetadataSources buildMetadata

List of usage examples for org.hibernate.boot MetadataSources buildMetadata

Introduction

In this page you can find the example usage for org.hibernate.boot MetadataSources buildMetadata.

Prototype

public Metadata buildMetadata(StandardServiceRegistry serviceRegistry) 

Source Link

Usage

From source file:com.booleanworks.peacockmantisshrimp.modules.hibernate.mongodbogm.HibernateOgmBootstapper.java

public boolean bootstrap() throws OgmHelperException {

    this.checkConfigurationAndUpdateStatus();

    if (this.getStatus() != Status.goodConfiguration) {
        Logger.getLogger(this.getClass().getCanonicalName()).log(Level.SEVERE,
                "Bootstrapper seems to be in a bad configuration... aborting.");
        return false;
    }/*www .  ja v  a 2  s  .  c  om*/

    if (this.getStatus() == Status.bootstrapping) {
        throw new OgmHelperException("already bootstrapping  !!");
    }

    this.setStatus(Status.bootstrapping);

    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();

    //And specific config
    ssrb.applySetting(OgmProperties.ENABLED, true);

    //assuming you are using JTA in a non container environment
    //ssrb.applySetting(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta");
    //assuming JBoss TransactionManager in standalone mode
    //ssrb.applySetting(AvailableSettings.JTA_PLATFORM, "JBossTS");
    //Using provided settings
    ssrb.applySettings(this.getSettings());

    if (this.getSettings().containsKey(OgmProperties.DATASTORE_PROVIDER)) {
        String providerValue = (String) this.getSettings().get(OgmProperties.DATASTORE_PROVIDER);

        if (providerValue.toLowerCase().equals("mongodb")) {
            Logger.getLogger(this.getClass().getCanonicalName()).log(Level.INFO,
                    "Detecting MongoDb provider.... doing additional checks");
            if (!this.getSettings().containsKey(MongoDBProperties.AUTHENTICATION_MECHANISM)) {
                AuthenticationMechanismType authenticationMechanismType = AuthenticationMechanismType.SCRAM_SHA_1;

                Logger.getLogger(this.getClass().getCanonicalName()).log(Level.WARNING,
                        "No MongoDb authentication mecanism set, patching configuration, setting to "
                                + authenticationMechanismType.name());
                this.getSettings().put(MongoDBProperties.AUTHENTICATION_MECHANISM, authenticationMechanismType);

                Logger.getLogger(this.getClass().getCanonicalName()).log(Level.WARNING,
                        "Current setings are: " + this.getSettings().toString());
            }
        }

    }

    //saving ssrb
    this.setStandardServiceRegistryBuilder(ssrb);

    //building registry
    StandardServiceRegistry registry = ssrb.build();
    if (registry == null) {
        throw new OgmHelperException("Failed to get a registry !!");
    }

    //saving registry
    this.setStandardServiceRegistry(registry);

    MetadataSources ms = new MetadataSources(registry);

    for (Class cEntity : this.getDeclaredEntities()) {
        ms.addAnnotatedClass(cEntity);
        Logger.getLogger(this.getClass().getCanonicalName()).log(Level.INFO,
                "Added entity: " + cEntity.getCanonicalName());
    }

    Metadata md = ms.buildMetadata(registry);

    SessionFactoryBuilder sfb = md.getSessionFactoryBuilder();

    OgmSessionFactory osf = sfb.unwrap(OgmSessionFactoryBuilder.class).build();

    //Saving the MetadataSources
    this.setMetadataSources(ms);

    //Saving SessionFactoryBuilder
    this.setSessionFactoryBuilder(sfb);

    //Savinf the OgmSessionFactory 
    this.setOgmSessionFactory(osf);

    this.setStatus(Status.ready);

    return true;
}

From source file:org.unitedinternet.cosmo.db.DbInitializer.java

License:Apache License

/**
 * Schema validation//  w ww  . jav  a 2 s  .  c  om
 */
private void validateSchema() {
    try {
        StandardServiceRegistry registry = localSessionFactory.getConfiguration()
                .getStandardServiceRegistryBuilder().build();
        MetadataSources sources = new MetadataSources(registry);
        sources.addPackage("org.unitedinternet.cosmo.model.hibernate");
        Metadata metadata = sources.buildMetadata(registry);
        new SchemaValidator().validate(metadata);
        LOG.info("schema validation passed");
    } catch (HibernateException e) {
        LOG.error("error validating schema", e);
        throw e;
    }
}