/** * 2017-05-24 · reworked from SpringHibernateJpaPersistenceProvider so that we can inject a custom * {@link EntityManagerFactoryBuilderImpl}; previous implementation that overrides * {@link InterceptorAwareHibernatePersistenceProvider#getEntityManagerFactoryBuilder} no longer works * as there are several paths with various arguments and the overloaded one was no longer called. */ @Override @SuppressWarnings("rawtypes") public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) { return new EntityManagerFactoryBuilderImpl(new PersistenceUnitInfoDescriptor(info), properties) { @Override protected void populate(SessionFactoryBuilder sfBuilder, StandardServiceRegistry ssr) { super.populate(sfBuilder, ssr); if (InterceptorAwareHibernatePersistenceProvider.this.interceptor != null) { LOGGER.warn("Installing our Spring managed interceptor."); sfBuilder.applyInterceptor(InterceptorAwareHibernatePersistenceProvider.this.interceptor); } } }.build(); }
private Block metadataBuilder() { return unit -> { MetadataBuilder mb = unit.get(MetadataBuilder.class); expect(mb.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)) .andReturn(mb); expect(mb.applyScanEnvironment(unit.get(ScanEnvironment.class))).andReturn(mb); SessionFactoryBuilder sfb = unit.mock(SessionFactoryBuilder.class); unit.registerMock(SessionFactoryBuilder.class, sfb); Metadata md = unit.mock(Metadata.class); expect(md.getSessionFactoryBuilder()).andReturn(sfb); expect(mb.build()).andReturn(md); unit.registerMock(Metadata.class, md); }; }
@Override protected void populate(SessionFactoryBuilder sessionFactoryBuilder, StandardServiceRegistry serviceRegistry) { serviceRegistry.getService(MetadataRef.class).setMetadata(this.getMetadata()); serviceRegistry.getService(CacheRegionFactory.class); serviceRegistry.getService(SdcctHsqlDialectResolver.class); super.populate(sessionFactoryBuilder, serviceRegistry); }
@SuppressWarnings("unchecked") private Block beanManager() { return unit -> { unit.mockStatic(GuiceBeanManager.class); BeanManager bm = unit.mock(BeanManager.class); unit.registerMock(BeanManager.class, bm); expect(GuiceBeanManager.beanManager(unit.capture(CompletableFuture.class))).andReturn(bm); SessionFactoryBuilder sfb = unit.get(SessionFactoryBuilder.class); expect(sfb.applyBeanManager(bm)).andReturn(sfb); }; }
private Block sessionFactoryBuilder(final String name) { return unit -> { SessionFactoryBuilder sfb = unit.get(SessionFactoryBuilder.class); expect(sfb.applyName(name)).andReturn(sfb); SessionFactoryImplementor sf = unit.mock(SessionFactoryImplementor.class); expect(sfb.build()).andReturn(sf); unit.registerMock(SessionFactory.class, sf); unit.registerMock(SessionFactoryImplementor.class, sf); unit.registerMock(EntityManagerFactory.class, sf); }; }
private SessionFactory newSessionFactory() { final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder() .enableAutoClose(); Integrator integrator = integrator(); if (integrator != null) { bsrb.applyIntegrator(integrator); } final BootstrapServiceRegistry bsr = bsrb.build(); final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bsr) .applySettings(properties()) .build(); final MetadataSources metadataSources = new MetadataSources(serviceRegistry); for (Class annotatedClass : entities()) { metadataSources.addAnnotatedClass(annotatedClass); } String[] packages = packages(); if (packages != null) { for (String annotatedPackage : packages) { metadataSources.addPackage(annotatedPackage); } } String[] resources = resources(); if (resources != null) { for (String resource : resources) { metadataSources.addResource(resource); } } final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(); metadataBuilder.enableNewIdentifierGeneratorSupport(true); metadataBuilder.applyImplicitNamingStrategy(ImplicitNamingStrategyLegacyJpaImpl.INSTANCE); MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build(); final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder(); Interceptor interceptor = interceptor(); if (interceptor != null) { sfb.applyInterceptor(interceptor); } return sfb.build(); }
@Override public SessionFactoryBuilder getSessionFactoryBuilder( MetadataImplementor metadataImplementor, SessionFactoryBuilderImplementor defaultBuilder ) { HibernateMetadata.metadataImplementor.set( metadataImplementor ); return defaultBuilder; }
private SessionFactory newSessionFactory() { final BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder() .enableAutoClose(); Integrator integrator = integrator(); if (integrator != null) { bsrb.applyIntegrator( integrator ); } final BootstrapServiceRegistry bsr = bsrb.build(); final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(bsr) .applySettings(properties()) .build(); final MetadataSources metadataSources = new MetadataSources(serviceRegistry); for (Class annotatedClass : entities()) { metadataSources.addAnnotatedClass(annotatedClass); } String[] packages = packages(); if (packages != null) { for (String annotatedPackage : packages) { metadataSources.addPackage(annotatedPackage); } } String[] resources = resources(); if (resources != null) { for (String resource : resources) { metadataSources.addResource(resource); } } final MetadataBuilder metadataBuilder = metadataSources.getMetadataBuilder(); metadataBuilder.enableNewIdentifierGeneratorSupport(true); metadataBuilder.applyImplicitNamingStrategy(ImplicitNamingStrategyLegacyJpaImpl.INSTANCE); final List<Type> additionalTypes = additionalTypes(); if (additionalTypes != null) { additionalTypes.stream().forEach(type -> { metadataBuilder.applyTypes((typeContributions, serviceRegistry1) -> { if(type instanceof BasicType) { typeContributions.contributeType((BasicType) type); } else if (type instanceof UserType ){ typeContributions.contributeType((UserType) type); } else if (type instanceof CompositeUserType) { typeContributions.contributeType((CompositeUserType) type); } }); }); } MetadataImplementor metadata = (MetadataImplementor) metadataBuilder.build(); final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder(); Interceptor interceptor = interceptor(); if(interceptor != null) { sfb.applyInterceptor(interceptor); } return sfb.build(); }
@SuppressWarnings("static-access") @Override public SessionFactoryBuilder getSessionFactoryBuilder(MetadataImplementor metadata, SessionFactoryBuilderImplementor defaultBuilder) { this.metadata = metadata; return defaultBuilder; // Just return the one provided in the argument itself. All we care about is the metadata :) }
@Override public void configure(final Env env, final Config conf, final Binder binder) { Key<DataSource> dskey = Key.get(DataSource.class, Names.named(name)); DataSource ds = env.get(dskey) .orElseThrow(() -> new NoSuchElementException("DataSource missing: " + dskey)); BootstrapServiceRegistryBuilder bsrb = new BootstrapServiceRegistryBuilder(); bsrb.applyIntegrator(new JpaIntegrator()); this.bsrb.accept(bsrb, conf); String ddl_auto = env.name().equals("dev") ? "update" : "none"; BootstrapServiceRegistry bsr = bsrb.build(); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder(bsr); ssrb.applySetting(AvailableSettings.HBM2DDL_AUTO, ddl_auto); ssrb.applySettings(settings(env, conf)); this.ssrb.accept(ssrb, conf); ssrb.applySetting(AvailableSettings.DATASOURCE, ds); ssrb.applySetting(org.hibernate.jpa.AvailableSettings.DELAY_CDI_ACCESS, true); StandardServiceRegistry serviceRegistry = ssrb.build(); MetadataSources sources = new MetadataSources(serviceRegistry); this.sources.forEach(src -> src.accept(sources, conf)); this.metaSources.accept(sources, conf); /** scan package? */ List<URL> packages = sources.getAnnotatedPackages() .stream() .map(pkg -> getClass().getResource("/" + pkg.replace('.', '/'))) .collect(Collectors.toList()); Metadata metadata = sources.getMetadataBuilder() .applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE) .applyScanEnvironment(new ScanEnvImpl(packages)) .build(); SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder(); this.sfb.accept(sfb, conf); sfb.applyName(name); CompletableFuture<Registry> registry = new CompletableFuture<>(); sfb.applyBeanManager(GuiceBeanManager.beanManager(registry)); SessionFactory sessionFactory = sfb.build(); this.sf.accept(sessionFactory, conf); Provider<Session> session = new SessionProvider(sessionFactory); ServiceKey serviceKey = env.serviceKey(); serviceKey.generate(SessionFactory.class, name, k -> binder.bind(k).toInstance(sessionFactory)); serviceKey.generate(EntityManagerFactory.class, name, k -> binder.bind(k).toInstance(sessionFactory)); /** Session/Entity Manager . */ serviceKey.generate(Session.class, name, k -> binder.bind(k).toProvider(session)); serviceKey.generate(EntityManager.class, name, k -> binder.bind(k).toProvider(session)); /** Unit of work . */ Provider<UnitOfWork> uow = new UnitOfWorkProvider(sessionFactory); serviceKey.generate(UnitOfWork.class, name, k -> binder.bind(k).toProvider(uow)); bindings.forEach(it -> it.accept(binder)); env.onStart(r -> { registry.complete(r); listeners.forEach(it -> it.accept((SessionFactoryImplementor) sessionFactory, r)); }); env.onStop(sessionFactory::close); }
/** * Configurer callback to apply advanced configuration while bootstrapping hibernate: * * @param configurer Configurer callback. * @return This module */ public <T> Hbm doWithSessionFactoryBuilder(final Consumer<SessionFactoryBuilder> configurer) { return doWithSessionFactoryBuilder((builder, conf) -> configurer.accept(builder)); }
/** * Configurer callback to apply advanced configuration while bootstrapping hibernate: * * @param configurer Configurer callback. * @return This module */ public <T> Hbm doWithSessionFactoryBuilder(final BiConsumer<SessionFactoryBuilder, Config> configurer) { this.sfb = configurer; return this; }