Search in sources :

Example 1 with MetadataBuilder

use of org.hibernate.boot.MetadataBuilder in project hibernate-orm by hibernate.

the class OsgiSessionFactoryService method buildSessionFactory.

private Object buildSessionFactory(Bundle requestingBundle, OsgiClassLoader osgiClassLoader) {
    final BootstrapServiceRegistryBuilder bsrBuilder = new BootstrapServiceRegistryBuilder();
    bsrBuilder.applyClassLoaderService(new OSGiClassLoaderServiceImpl(osgiClassLoader, osgiServiceUtil));
    final Integrator[] integrators = osgiServiceUtil.getServiceImpls(Integrator.class);
    for (Integrator integrator : integrators) {
        bsrBuilder.applyIntegrator(integrator);
    }
    final StrategyRegistrationProvider[] strategyRegistrationProviders = osgiServiceUtil.getServiceImpls(StrategyRegistrationProvider.class);
    for (StrategyRegistrationProvider strategyRegistrationProvider : strategyRegistrationProviders) {
        bsrBuilder.applyStrategySelectors(strategyRegistrationProvider);
    }
    final BootstrapServiceRegistry bsr = bsrBuilder.build();
    final StandardServiceRegistryBuilder ssrBuilder = new StandardServiceRegistryBuilder(bsr);
    // Allow bundles to put the config file somewhere other than the root level.
    final BundleWiring bundleWiring = (BundleWiring) requestingBundle.adapt(BundleWiring.class);
    final Collection<String> cfgResources = bundleWiring.listResources("/", "hibernate.cfg.xml", BundleWiring.LISTRESOURCES_RECURSE);
    if (cfgResources.size() == 0) {
        ssrBuilder.configure();
    } else {
        if (cfgResources.size() > 1) {
            LOG.warn("Multiple hibernate.cfg.xml files found in the persistence bundle.  Using the first one discovered.");
        }
        String cfgResource = "/" + cfgResources.iterator().next();
        ssrBuilder.configure(cfgResource);
    }
    ssrBuilder.applySetting(AvailableSettings.JTA_PLATFORM, osgiJtaPlatform);
    final StandardServiceRegistry ssr = ssrBuilder.build();
    final MetadataBuilder metadataBuilder = new MetadataSources(ssr).getMetadataBuilder();
    final TypeContributor[] typeContributors = osgiServiceUtil.getServiceImpls(TypeContributor.class);
    for (TypeContributor typeContributor : typeContributors) {
        metadataBuilder.applyTypes(typeContributor);
    }
    return metadataBuilder.build().buildSessionFactory();
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataBuilder(org.hibernate.boot.MetadataBuilder) BundleWiring(org.osgi.framework.wiring.BundleWiring) MetadataSources(org.hibernate.boot.MetadataSources) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) TypeContributor(org.hibernate.boot.model.TypeContributor) StrategyRegistrationProvider(org.hibernate.boot.registry.selector.StrategyRegistrationProvider) Integrator(org.hibernate.integrator.spi.Integrator) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Example 2 with MetadataBuilder

use of org.hibernate.boot.MetadataBuilder in project hibernate-orm by hibernate.

the class BaseNonConfigCoreFunctionalTestCase method buildResources.

protected void buildResources() {
    final StandardServiceRegistryBuilder ssrb = constructStandardServiceRegistryBuilder();
    serviceRegistry = ssrb.build();
    afterStandardServiceRegistryBuilt(serviceRegistry);
    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    applyMetadataSources(metadataSources);
    afterMetadataSourcesApplied(metadataSources);
    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
    initialize(metadataBuilder);
    configureMetadataBuilder(metadataBuilder);
    metadata = (MetadataImplementor) metadataBuilder.build();
    applyCacheSettings(metadata);
    afterMetadataBuilt(metadata);
    final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
    initialize(sfb, metadata);
    configureSessionFactoryBuilder(sfb);
    sessionFactory = (SessionFactoryImplementor) sfb.build();
    afterSessionFactoryBuilt(sessionFactory);
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataBuilder(org.hibernate.boot.MetadataBuilder) SessionFactoryBuilder(org.hibernate.boot.SessionFactoryBuilder) MetadataSources(org.hibernate.boot.MetadataSources)

Example 3 with MetadataBuilder

use of org.hibernate.boot.MetadataBuilder in project hibernate-orm by hibernate.

the class IdentityGeneratorExtendsTest method testIdentifierGeneratorExtendsIdentityGenerator.

@Test
public void testIdentifierGeneratorExtendsIdentityGenerator() {
    final MetadataSources sources = new MetadataSources(serviceRegistry());
    sources.addAnnotatedClass(EntityBean.class);
    final MetadataBuilder builder = sources.getMetadataBuilder();
    final Metadata metadata = builder.build();
    for (final Namespace ns : metadata.getDatabase().getNamespaces()) {
        for (final org.hibernate.mapping.Table table : ns.getTables()) {
            final KeyValue value = table.getIdentifierValue();
            assertNotNull("IdentifierValue was null", value);
            assertTrue(value.isIdentityColumn(metadata.getIdentifierGeneratorFactory(), getDialect()));
        }
    }
    Session s = openSession();
    s.beginTransaction();
    s.save(new EntityBean());
    s.getTransaction().commit();
    s.close();
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) MetadataBuilder(org.hibernate.boot.MetadataBuilder) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) Namespace(org.hibernate.boot.model.relational.Namespace) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with MetadataBuilder

use of org.hibernate.boot.MetadataBuilder in project hibernate-orm by hibernate.

the class Tests method testJpaJoinColumnPhysicalNaming.

@Test
@TestForIssue(jiraKey = "HHH-9961")
public void testJpaJoinColumnPhysicalNaming() {
    final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().applySettings(Environment.getProperties()).build();
    try {
        final MetadataSources metadataSources = new MetadataSources(ssr);
        metadataSources.addAnnotatedClass(Language.class);
        final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
        metadataBuilder.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE);
        metadataBuilder.applyPhysicalNamingStrategy(PhysicalNamingStrategyImpl.INSTANCE);
        final Metadata metadata = metadataBuilder.build();
        ((MetadataImplementor) metadata).validate();
        final PersistentClass languageBinding = metadata.getEntityBinding(Language.class.getName());
        final Property property = languageBinding.getProperty("fallBack");
        Iterator itr = property.getValue().getColumnIterator();
        assertTrue(itr.hasNext());
        final Column column = (Column) itr.next();
        assertFalse(itr.hasNext());
        assertEquals("C_FALLBACK_ID", column.getName().toUpperCase(Locale.ROOT));
    } finally {
        StandardServiceRegistryBuilder.destroy(ssr);
    }
}
Also used : StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) MetadataBuilder(org.hibernate.boot.MetadataBuilder) Column(org.hibernate.mapping.Column) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) Iterator(java.util.Iterator) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) Property(org.hibernate.mapping.Property) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 5 with MetadataBuilder

use of org.hibernate.boot.MetadataBuilder in project hibernate-orm by hibernate.

the class SchemaUpdate method buildMetadata.

private static MetadataImplementor buildMetadata(CommandLineArgs parsedArgs, ServiceRegistry serviceRegistry) throws Exception {
    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    for (String filename : parsedArgs.hbmXmlFiles) {
        metadataSources.addFile(filename);
    }
    for (String filename : parsedArgs.jarFiles) {
        metadataSources.addJar(new File(filename));
    }
    final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder();
    final StrategySelector strategySelector = serviceRegistry.getService(StrategySelector.class);
    if (parsedArgs.implicitNamingStrategyImplName != null) {
        metadataBuilder.applyImplicitNamingStrategy(strategySelector.resolveStrategy(ImplicitNamingStrategy.class, parsedArgs.implicitNamingStrategyImplName));
    }
    if (parsedArgs.physicalNamingStrategyImplName != null) {
        metadataBuilder.applyPhysicalNamingStrategy(strategySelector.resolveStrategy(PhysicalNamingStrategy.class, parsedArgs.physicalNamingStrategyImplName));
    }
    return (MetadataImplementor) metadataBuilder.build();
}
Also used : ImplicitNamingStrategy(org.hibernate.boot.model.naming.ImplicitNamingStrategy) MetadataBuilder(org.hibernate.boot.MetadataBuilder) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) File(java.io.File) StrategySelector(org.hibernate.boot.registry.selector.spi.StrategySelector) PhysicalNamingStrategy(org.hibernate.boot.model.naming.PhysicalNamingStrategy)

Aggregations

MetadataBuilder (org.hibernate.boot.MetadataBuilder)15 MetadataSources (org.hibernate.boot.MetadataSources)14 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)10 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)7 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)7 Metadata (org.hibernate.boot.Metadata)5 File (java.io.File)4 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)4 Test (org.junit.Test)4 SessionFactoryBuilder (org.hibernate.boot.SessionFactoryBuilder)3 ImplicitNamingStrategy (org.hibernate.boot.model.naming.ImplicitNamingStrategy)3 PhysicalNamingStrategy (org.hibernate.boot.model.naming.PhysicalNamingStrategy)3 StrategySelector (org.hibernate.boot.registry.selector.spi.StrategySelector)3 ServiceRegistry (org.hibernate.service.ServiceRegistry)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 BuildException (org.apache.tools.ant.BuildException)2 HibernateException (org.hibernate.HibernateException)2 TypeContributor (org.hibernate.boot.model.TypeContributor)2 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)2