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 public SessionFactory buildFactory(String configFile) { try { //configuration = new Configuration().configure(configFile); // StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() // .applySettings(configuration.getProperties()); // SessionFactory factory2 = configuration.buildSessionFactory(builder.build()); StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder() .configure(configFile) .build(); MetadataSources metadataSources = new MetadataSources( standardRegistry ); DomainScanner instanceFromProperties = getScanner(configFile); List<Class<? extends BaseDatabaseDomain>> scan = instanceFromProperties.scan(configFile); for(Class<? extends BaseDatabaseDomain> temp:scan){ metadataSources.addAnnotatedClass(temp); } // new HibernateUtil().scanDomain(metadataSources); Metadata metadata = metadataSources.getMetadataBuilder() .applyImplicitNamingStrategy( ImplicitNamingStrategyJpaCompliantImpl.INSTANCE ) .build(); SessionFactory factory2 = metadata.getSessionFactoryBuilder().build(); return factory2; /*ProxoolConnectionProvider aConnectionProvider;*/ } catch (Exception e) { steed.util.logging.LoggerFactory.getLogger().error("创建sessionFactory失败",e); } return null; }
@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); }