Search in sources :

Example 51 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project jbosstools-hibernate by jbosstools.

the class FacadeFactoryTest method testCreateHQLQueryPlan.

@Test
public void testCreateHQLQueryPlan() {
    final Collection<PersistentClass> entityBindings = new ArrayList<PersistentClass>();
    final StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder();
    standardServiceRegistryBuilder.applySetting("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    final StandardServiceRegistry serviceRegistry = standardServiceRegistryBuilder.build();
    final MetadataSources metadataSources = new MetadataSources(serviceRegistry);
    final MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    Table t = new Table("FOO");
    Column c = new Column("foo");
    t.addColumn(c);
    PrimaryKey key = new PrimaryKey(t);
    key.addColumn(c);
    t.setPrimaryKey(key);
    SimpleValue sv = new SimpleValue(metadata);
    sv.setNullValue("null");
    sv.setTypeName(Integer.class.getName());
    sv.setTable(t);
    sv.addColumn(c);
    final RootClass rc = new RootClass(null);
    rc.setEntityName("foo");
    rc.setJpaEntityName("foo");
    rc.setIdentifier(sv);
    rc.setTable(t);
    entityBindings.add(rc);
    MetadataImplementor wrapper = (MetadataImplementor) Proxy.newProxyInstance(facadeFactory.getClassLoader(), new Class[] { MetadataImplementor.class }, new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if ("getEntityBinding".equals(method.getName()) && args != null && args.length == 1 && "foo".equals(args[0])) {
                return rc;
            } else if ("getEntityBindings".equals(method.getName())) {
                return entityBindings;
            }
            return method.invoke(metadata, args);
        }
    });
    SessionFactoryImpl sfi = new SessionFactoryImpl(wrapper, new SessionFactoryOptionsBuilder(serviceRegistry));
    Map<String, Filter> filters = Collections.emptyMap();
    HQLQueryPlan hqlQueryPlan = new HQLQueryPlan("from foo", false, filters, sfi);
    IHQLQueryPlan facade = facadeFactory.createHQLQueryPlan(hqlQueryPlan);
    Assert.assertSame(hqlQueryPlan, ((IFacade) facade).getTarget());
}
Also used : RootClass(org.hibernate.mapping.RootClass) ITable(org.jboss.tools.hibernate.runtime.spi.ITable) Table(org.hibernate.mapping.Table) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryOptionsBuilder(org.hibernate.boot.internal.SessionFactoryOptionsBuilder) ArrayList(java.util.ArrayList) MetadataSources(org.hibernate.boot.MetadataSources) PrimaryKey(org.hibernate.mapping.PrimaryKey) IPrimaryKey(org.jboss.tools.hibernate.runtime.spi.IPrimaryKey) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) Method(java.lang.reflect.Method) IHQLQueryPlan(org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan) InvocationHandler(java.lang.reflect.InvocationHandler) SimpleValue(org.hibernate.mapping.SimpleValue) HQLQueryPlan(org.hibernate.engine.query.spi.HQLQueryPlan) IHQLQueryPlan(org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan) IColumn(org.jboss.tools.hibernate.runtime.spi.IColumn) Column(org.hibernate.mapping.Column) ITableFilter(org.jboss.tools.hibernate.runtime.spi.ITableFilter) TableFilter(org.hibernate.cfg.reveng.TableFilter) Filter(org.hibernate.Filter) PersistentClass(org.hibernate.mapping.PersistentClass) IPOJOClass(org.jboss.tools.hibernate.runtime.spi.IPOJOClass) RootClass(org.hibernate.mapping.RootClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) POJOClass(org.hibernate.tool.hbm2x.pojo.POJOClass) SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) PersistentClass(org.hibernate.mapping.PersistentClass) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 52 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project jbosstools-hibernate by jbosstools.

the class ColumnFacadeTest method testGetValue.

public void testGetValue() throws Exception {
    Value targetValue = null;
    column.setValue(targetValue);
    Assert.assertNull(columnFacade.getValue());
    StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
    ssrb.applySetting(Environment.DIALECT, "org.hibernate.dialect.H2Dialect");
    StandardServiceRegistry ssr = ssrb.build();
    MetadataSources metadataSources = new MetadataSources(ssr);
    MetadataImplementor metadata = (MetadataImplementor) metadataSources.buildMetadata();
    targetValue = new SimpleValue(metadata);
    column.setValue(targetValue);
    IValue value = columnFacade.getValue();
    Assert.assertNotNull(value);
    Assert.assertEquals(targetValue, ((IFacade) value).getTarget());
}
Also used : IValue(org.jboss.tools.hibernate.runtime.spi.IValue) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SimpleValue(org.hibernate.mapping.SimpleValue) IValue(org.jboss.tools.hibernate.runtime.spi.IValue) Value(org.hibernate.mapping.Value) MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) SimpleValue(org.hibernate.mapping.SimpleValue)

Example 53 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class DelayedMixedAccessTest method testDelayedMixedAccess.

@Test
public void testDelayedMixedAccess() {
    try (final SeContainer cdiContainer = Helper.createSeContainer()) {
        BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager()).applySetting(AvailableSettings.DELAY_CDI_ACCESS, "true").build();
        final BeanContainer beanContainer = CdiBeanContainerBuilder.fromBeanManagerReference(cdiContainer.getBeanManager(), ssr);
        assertThat(beanContainer, instanceOf(CdiBeanContainerDelayedAccessImpl.class));
        final ContainedBean<HostedBean> hostedBean = beanContainer.getBean(HostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(hostedBean, notNullValue());
        assertThat(hostedBean.getBeanInstance(), notNullValue());
        assertThat(hostedBean.getBeanInstance().getInjectedHostedBean(), notNullValue());
        final ContainedBean<NonHostedBean> nonHostedBean = beanContainer.getBean(NonHostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(nonHostedBean, notNullValue());
        assertThat(nonHostedBean.getBeanInstance(), notNullValue());
    }
}
Also used : CdiBeanContainerDelayedAccessImpl(org.hibernate.resource.beans.container.internal.CdiBeanContainerDelayedAccessImpl) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) BeanContainer(org.hibernate.resource.beans.container.spi.BeanContainer) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 54 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class ImmediateMixedAccessTests method testImmediateMixedAccess.

@Test
public void testImmediateMixedAccess() {
    try (final SeContainer cdiContainer = Helper.createSeContainer()) {
        BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager()).build();
        final BeanContainer beanContainer = CdiBeanContainerBuilder.fromBeanManagerReference(cdiContainer.getBeanManager(), ssr);
        assertThat(beanContainer, instanceOf(CdiBeanContainerImmediateAccessImpl.class));
        final ContainedBean<HostedBean> hostedBean = beanContainer.getBean(HostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(hostedBean, notNullValue());
        assertThat(hostedBean.getBeanInstance(), notNullValue());
        assertThat(hostedBean.getBeanInstance().getInjectedHostedBean(), notNullValue());
        final ContainedBean<NonHostedBean> nonHostedBean = beanContainer.getBean(NonHostedBean.class, this, FallbackBeanInstanceProducer.INSTANCE);
        assertThat(nonHostedBean, notNullValue());
        assertThat(nonHostedBean.getBeanInstance(), notNullValue());
    }
}
Also used : BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) BeanContainer(org.hibernate.resource.beans.container.spi.BeanContainer) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) CdiBeanContainerImmediateAccessImpl(org.hibernate.resource.beans.container.internal.CdiBeanContainerImmediateAccessImpl) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Example 55 with StandardServiceRegistry

use of org.hibernate.boot.registry.StandardServiceRegistry in project hibernate-orm by hibernate.

the class DelayedCdiHostedConverterTest method testIt.

@Test
public void testIt() {
    MonitorBean.reset();
    final SeContainerInitializer cdiInitializer = SeContainerInitializer.newInstance().disableDiscovery().addBeanClasses(MonitorBean.class, ConverterBean.class);
    try (final SeContainer cdiContainer = cdiInitializer.initialize()) {
        BootstrapServiceRegistry bsr = new BootstrapServiceRegistryBuilder().build();
        final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder(bsr).applySetting(AvailableSettings.HBM2DDL_AUTO, Action.CREATE_DROP).applySetting(AvailableSettings.CDI_BEAN_MANAGER, cdiContainer.getBeanManager()).applySetting(AvailableSettings.DELAY_CDI_ACCESS, "true").build();
        final SessionFactoryImplementor sessionFactory;
        try {
            sessionFactory = (SessionFactoryImplementor) new MetadataSources(ssr).addAnnotatedClass(TheEntity.class).buildMetadata().getSessionFactoryBuilder().build();
        } catch (Exception e) {
            StandardServiceRegistryBuilder.destroy(ssr);
            throw e;
        }
        // The CDI bean should not have been built immediately...
        assertFalse(MonitorBean.wasInstantiated());
        assertEquals(0, MonitorBean.currentFromDbCount());
        assertEquals(0, MonitorBean.currentToDbCount());
        try {
            inTransaction(sessionFactory, session -> session.persist(new TheEntity(1, "me", 5)));
            // The CDI bean should have been built on first use
            assertTrue(MonitorBean.wasInstantiated());
            assertEquals(0, MonitorBean.currentFromDbCount());
            assertEquals(1, MonitorBean.currentToDbCount());
            inTransaction(sessionFactory, session -> {
                TheEntity it = session.find(TheEntity.class, 1);
                assertNotNull(it);
            });
            assertEquals(1, MonitorBean.currentFromDbCount());
            assertEquals(1, MonitorBean.currentToDbCount());
        } finally {
            inTransaction(sessionFactory, session -> {
                session.createQuery("delete TheEntity").executeUpdate();
            });
            sessionFactory.close();
        }
    }
}
Also used : TheEntity(org.hibernate.test.cdi.converters.TheEntity) BootstrapServiceRegistryBuilder(org.hibernate.boot.registry.BootstrapServiceRegistryBuilder) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) MetadataSources(org.hibernate.boot.MetadataSources) SeContainer(javax.enterprise.inject.se.SeContainer) BootstrapServiceRegistry(org.hibernate.boot.registry.BootstrapServiceRegistry) SeContainerInitializer(javax.enterprise.inject.se.SeContainerInitializer) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Test(org.junit.Test)

Aggregations

StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)169 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)152 Test (org.junit.Test)121 MetadataSources (org.hibernate.boot.MetadataSources)118 Metadata (org.hibernate.boot.Metadata)56 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)39 PersistentClass (org.hibernate.mapping.PersistentClass)39 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)25 TestForIssue (org.hibernate.testing.TestForIssue)24 BootstrapServiceRegistryBuilder (org.hibernate.boot.registry.BootstrapServiceRegistryBuilder)23 Properties (java.util.Properties)20 BootstrapServiceRegistry (org.hibernate.boot.registry.BootstrapServiceRegistry)20 SchemaUpdate (org.hibernate.tool.hbm2ddl.SchemaUpdate)15 SessionFactory (org.hibernate.SessionFactory)14 Column (org.hibernate.mapping.Column)14 Property (org.hibernate.mapping.Property)14 Session (org.hibernate.Session)13 SchemaExport (org.hibernate.tool.hbm2ddl.SchemaExport)12 IdentifierGenerator (org.hibernate.id.IdentifierGenerator)11 RootClass (org.hibernate.mapping.RootClass)11