Java 类org.hibernate.SessionBuilder 实例源码

项目:lams    文件:AbstractCurrentSessionContext.java   
protected SessionBuilder baseSessionBuilder() {
    final SessionBuilder builder = factory.withOptions();
    final CurrentTenantIdentifierResolver resolver = factory.getCurrentTenantIdentifierResolver();
    if ( resolver != null ) {
        builder.tenantIdentifier( resolver.resolveCurrentTenantIdentifier() );
    }
    return builder;
}
项目:spring4-understanding    文件:HibernateTransactionManagerTests.java   
@Test
public void testTransactionCommitWithEntityInterceptor() throws Exception {
    Interceptor entityInterceptor = mock(Interceptor.class);
    Connection con = mock(Connection.class);
    final SessionFactory sf = mock(SessionFactory.class);
    ImplementingSession session = mock(ImplementingSession.class);
    SessionBuilder options = mock(SessionBuilder.class);
    Transaction tx = mock(Transaction.class);

    given(sf.withOptions()).willReturn(options);
    given(options.interceptor(entityInterceptor)).willReturn(options);
    given(options.openSession()).willReturn(session);
    given(session.beginTransaction()).willReturn(tx);
    given(session.isOpen()).willReturn(true);
    given(session.isConnected()).willReturn(true);
    given(session.connection()).willReturn(con);

    HibernateTransactionManager tm = new HibernateTransactionManager(sf);
    tm.setEntityInterceptor(entityInterceptor);
    tm.setAllowResultAccessAfterCompletion(true);
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
        }
    });

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

    verify(session).close();
    verify(tx).commit();
}
项目:class-guard    文件:HibernateTransactionManagerTests.java   
@Test
public void testTransactionCommitWithEntityInterceptor() throws Exception {
    Interceptor entityInterceptor = mock(Interceptor.class);
    Connection con = mock(Connection.class);
    final SessionFactory sf = mock(SessionFactory.class);
    ImplementingSession session = mock(ImplementingSession.class);
    SessionBuilder options = mock(SessionBuilder.class);
    Transaction tx = mock(Transaction.class);

    given(sf.withOptions()).willReturn(options);
    given(options.interceptor(entityInterceptor)).willReturn(options);
    given(options.openSession()).willReturn(session);
    given(session.beginTransaction()).willReturn(tx);
    given(session.isOpen()).willReturn(true);
    given(session.isConnected()).willReturn(true);
    given(session.connection()).willReturn(con);

    HibernateTransactionManager tm = new HibernateTransactionManager(sf);
    tm.setEntityInterceptor(entityInterceptor);
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

    Object result = tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
        }
    });

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

    verify(session).close();
    verify(tx).commit();
}
项目:lams    文件:SessionImpl.java   
@Override
public SessionBuilder clearEventListeners() {
    super.clearEventListeners();
    return this;
}
项目:lams    文件:SessionFactoryImpl.java   
@Override
public SessionBuilder owner(SessionOwner sessionOwner) {
    this.sessionOwner = sessionOwner;
    return this;
}
项目:lams    文件:SessionFactoryImpl.java   
@Override
public SessionBuilder noInterceptor() {
    this.interceptor = EmptyInterceptor.INSTANCE;
    return this;
}
项目:lams    文件:SessionFactoryImpl.java   
@Override
public SessionBuilder eventListeners(SessionEventListener... listeners) {
    Collections.addAll( this.listeners, listeners );
    return this;
}
项目:lams    文件:SessionFactoryImpl.java   
@Override
public SessionBuilder clearEventListeners() {
    listeners.clear();
    return this;
}
项目:spring4-understanding    文件:HibernateTransactionManagerTests.java   
@Test
public void testTransactionCommitWithEntityInterceptorBeanName() throws Exception {
    Interceptor entityInterceptor = mock(Interceptor.class);
    Interceptor entityInterceptor2 = mock(Interceptor.class);
    Connection con = mock(Connection.class);
    final SessionFactory sf = mock(SessionFactory.class);
    ImplementingSession session = mock(ImplementingSession.class);
    SessionBuilder options = mock(SessionBuilder.class);
    Transaction tx = mock(Transaction.class);

    given(sf.withOptions()).willReturn(options);
    given(options.interceptor(entityInterceptor)).willReturn(options);
    given(options.interceptor(entityInterceptor2)).willReturn(options);
    given(options.openSession()).willReturn(session);
    given(session.beginTransaction()).willReturn(tx);
    given(session.isOpen()).willReturn(true);
    given(session.isConnected()).willReturn(true);
    given(session.connection()).willReturn(con);

    BeanFactory beanFactory = mock(BeanFactory.class);
    given(beanFactory.getBean("entityInterceptor", Interceptor.class)).willReturn(
            entityInterceptor, entityInterceptor2);

    HibernateTransactionManager tm = new HibernateTransactionManager(sf);
    tm.setEntityInterceptorBeanName("entityInterceptor");
    tm.setBeanFactory(beanFactory);

    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

    for (int i = 0; i < 2; i++) {
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            public void doInTransactionWithoutResult(TransactionStatus status) {
                assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
            }
        });
    }

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

    verify(session, times(2)).close();
    verify(tx, times(2)).commit();
}
项目:ignite    文件:CacheHibernateStoreFactorySelfTest.java   
/** {@inheritDoc} */
@Override public SessionBuilder withOptions() {
    return null;
}
项目:ignite    文件:CacheHibernateStoreFactorySelfTest.java   
/** {@inheritDoc} */
@Override public SessionBuilder withOptions() {
    return null;
}
项目:class-guard    文件:HibernateTransactionManagerTests.java   
@Test
public void testTransactionCommitWithEntityInterceptorBeanName() throws Exception {
    Interceptor entityInterceptor = mock(Interceptor.class);
    Interceptor entityInterceptor2 = mock(Interceptor.class);
    Connection con = mock(Connection.class);
    final SessionFactory sf = mock(SessionFactory.class);
    ImplementingSession session = mock(ImplementingSession.class);
    SessionBuilder options = mock(SessionBuilder.class);
    Transaction tx = mock(Transaction.class);

    given(sf.withOptions()).willReturn(options);
    given(options.interceptor(entityInterceptor)).willReturn(options);
    given(options.interceptor(entityInterceptor2)).willReturn(options);
    given(options.openSession()).willReturn(session);
    given(session.beginTransaction()).willReturn(tx);
    given(session.isOpen()).willReturn(true);
    given(session.isConnected()).willReturn(true);
    given(session.connection()).willReturn(con);

    BeanFactory beanFactory = mock(BeanFactory.class);
    given(beanFactory.getBean("entityInterceptor", Interceptor.class)).willReturn(
            entityInterceptor, entityInterceptor2);

    HibernateTransactionManager tm = new HibernateTransactionManager(sf);
    tm.setEntityInterceptorBeanName("entityInterceptor");
    tm.setBeanFactory(beanFactory);

    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

    for (int i = 0; i < 2; i++) {
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            public void doInTransactionWithoutResult(TransactionStatus status) {
                assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sf));
            }
        });
    }

    assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sf));
    assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());

    verify(session, times(2)).close();
    verify(tx, times(2)).commit();
}
项目:hibernate-dynamic-dialects    文件:SessionFactoryImpl.java   
@Override
public SessionBuilder owner(SessionOwner sessionOwner) {
    this.sessionOwner = sessionOwner;
    return this;
}
项目:hibernate-dynamic-dialects    文件:SessionFactoryImpl.java   
@Override
public SessionBuilder noInterceptor() {
    this.interceptor = EmptyInterceptor.INSTANCE;
    return this;
}
项目:lams    文件:SessionBuilderImplementor.java   
/**
 * Adds the session owner to the session options
 *
 * @param sessionOwner The session owner.
 *
 * @return {@code this}, for method chaining
 */
public SessionBuilder owner(SessionOwner sessionOwner);