public class HbmPatch implements Jooby.Module {
...
private BiConsumer<SessionBuilder, Config> sb = NOOP;
...
public <T> HbmPatch doWithSessionBuilder( final BiConsumer<SessionBuilder, Config> configurer) { this.sb = configurer; return this; }
public <T> HbmPatch doWithSessionBuilder(final Consumer<SessionBuilder> configurer) { return doWithSessionBuilder((builder, conf) -> configurer.accept(builder)); }
@Override public void configure(final Env env, final Config conf, final Binder binder) {
...
Function<SessionFactory, Session> sp = sf -> { final SessionBuilder builder = sf.withOptions(); sb.accept(builder, conf); return builder.openSession(); };
Provider<Session> session = new SessionProviderPatch(sessionFactory, sp);
...
/** Unit of work . */ Provider<UnitOfWork> uow = new UnitOfWorkProviderPatch(sessionFactory, sp);
... }}
public class SessionProviderPatch implements Provider<Session> {
private final SessionFactory sf; private final Function<SessionFactory, Session> sp;
public SessionProviderPatch(final SessionFactory sf, final Function<SessionFactory, Session> sp) { this.sf = sf; this.sp = sp; }
@Override public Session get() { return ManagedSessionContext.hasBind(sf) ? sf.getCurrentSession() : sp.apply(sf); }}public class UnitOfWorkProviderPatch implements Provider<UnitOfWork> {
private final SessionFactory sf; private final Function<SessionFactory, Session> sp;
public UnitOfWorkProviderPatch(final SessionFactory sf, final Function<SessionFactory, Session> sp) { this.sf = sf; this.sp = sp; }
@Override public UnitOfWork get() { if (ManagedSessionContext.hasBind(sf)) { return new ChildUnitOfWork(sf.getCurrentSession()); } else { return new RootUnitOfWork(sp.apply(sf)); } }} use(new HbmPatch("db") .doWithSessionBuilder(b -> b.interceptor(new ActionLogInterceptor(require(ActionLogManager.class)))) .scan("my.package"));