Search in sources :

Example 81 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class TransactionsTest method bmt.

@Test
public void bmt() {
    //tag::transactions-api-bmt-example[]
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jta").build();
    Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Customer.class).getMetadataBuilder().build();
    SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
    // Note: depending on the JtaPlatform used and some optional settings,
    // the underlying transactions here will be controlled through either
    // the JTA TransactionManager or UserTransaction
    Session session = sessionFactory.openSession();
    try {
        // Assuming a JTA transaction is not already active,
        // this call the TM/UT begin method.  If a JTA
        // transaction is already active, we remember that
        // the Transaction associated with the Session did
        // not "initiate" the JTA transaction and will later
        // nop-op the commit and rollback calls...
        session.getTransaction().begin();
        session.persist(new Customer());
        Customer customer = (Customer) session.createQuery("select c from Customer c").uniqueResult();
        // calls TM/UT commit method, assuming we are initiator.
        session.getTransaction().commit();
    } catch (Exception e) {
        // where the exception happened
        if (session.getTransaction().getStatus() == TransactionStatus.ACTIVE || session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK) {
            // calls TM/UT commit method, assuming we are initiator;
            // otherwise marks the JTA transaction for rollback only
            session.getTransaction().rollback();
        }
    // handle the underlying error
    } finally {
        session.close();
        sessionFactory.close();
    }
//end::transactions-api-bmt-example[]
}
Also used : SessionFactory(org.hibernate.SessionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Session(org.hibernate.Session) Test(org.junit.Test)

Example 82 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class ThreadLocalSessionContext method bind.

/**
	 * Associates the given session with the current thread of execution.
	 *
	 * @param session The session to bind.
	 */
public static void bind(org.hibernate.Session session) {
    final SessionFactory factory = session.getSessionFactory();
    cleanupAnyOrphanedSession(factory);
    doBind(session, factory);
}
Also used : SessionFactory(org.hibernate.SessionFactory)

Example 83 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class HibernateAnnotationMappingTest method testUniqueConstraintAnnotationOnNaturalIds.

@Test
@TestForIssue(jiraKey = "HHH-7446")
public void testUniqueConstraintAnnotationOnNaturalIds() throws Exception {
    Configuration configuration = new Configuration();
    configuration.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    configuration.addAnnotatedClass(Month.class);
    SessionFactory sf = null;
    try {
        sf = configuration.buildSessionFactory();
        sf.close();
    } catch (ConcurrentModificationException e) {
        fail(e.toString());
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) ConcurrentModificationException(java.util.ConcurrentModificationException) Configuration(org.hibernate.cfg.Configuration) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 84 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class CriterionTest method testIlikeRendering.

@Test
public void testIlikeRendering() {
    SessionFactory sf = new Configuration().addAnnotatedClass(IrrelevantEntity.class).setProperty(AvailableSettings.DIALECT, IlikeSupportingDialect.class.getName()).setProperty(Environment.HBM2DDL_AUTO, "create-drop").buildSessionFactory();
    try {
        final Criteria criteria = sf.openSession().createCriteria(IrrelevantEntity.class);
        final CriteriaQueryTranslator translator = new CriteriaQueryTranslator((SessionFactoryImplementor) sf, (CriteriaImpl) criteria, IrrelevantEntity.class.getName(), "a");
        final Criterion ilikeExpression = Restrictions.ilike("name", "abc");
        final String ilikeExpressionSqlFragment = ilikeExpression.toSqlString(criteria, translator);
        assertEquals("a.name insensitiveLike ?", ilikeExpressionSqlFragment);
    } finally {
        sf.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) Criterion(org.hibernate.criterion.Criterion) IrrelevantEntity(org.hibernate.IrrelevantEntity) Criteria(org.hibernate.Criteria) CriteriaQueryTranslator(org.hibernate.loader.criteria.CriteriaQueryTranslator) Test(org.junit.Test)

Example 85 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class CriterionTest method testIlikeMimicing.

@Test
public void testIlikeMimicing() {
    SessionFactory sf = new Configuration().addAnnotatedClass(IrrelevantEntity.class).setProperty(AvailableSettings.DIALECT, NonIlikeSupportingDialect.class.getName()).setProperty(Environment.HBM2DDL_AUTO, "create-drop").buildSessionFactory();
    try {
        final Criteria criteria = sf.openSession().createCriteria(IrrelevantEntity.class);
        final CriteriaQueryTranslator translator = new CriteriaQueryTranslator((SessionFactoryImplementor) sf, (CriteriaImpl) criteria, IrrelevantEntity.class.getName(), "a");
        final Criterion ilikeExpression = Restrictions.ilike("name", "abc");
        final String ilikeExpressionSqlFragment = ilikeExpression.toSqlString(criteria, translator);
        assertEquals("lowLowLow(a.name) like ?", ilikeExpressionSqlFragment);
    } finally {
        sf.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) Criterion(org.hibernate.criterion.Criterion) IrrelevantEntity(org.hibernate.IrrelevantEntity) Criteria(org.hibernate.Criteria) CriteriaQueryTranslator(org.hibernate.loader.criteria.CriteriaQueryTranslator) Test(org.junit.Test)

Aggregations

SessionFactory (org.hibernate.SessionFactory)108 Test (org.junit.Test)62 Session (org.hibernate.Session)50 Configuration (org.hibernate.cfg.Configuration)35 Transaction (org.hibernate.Transaction)20 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)19 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)13 MetadataSources (org.hibernate.boot.MetadataSources)11 HibernateEntityManagerFactory (org.hibernate.jpa.HibernateEntityManagerFactory)9 TestForIssue (org.hibernate.testing.TestForIssue)9 Properties (java.util.Properties)8 Query (org.hibernate.Query)8 Metadata (org.hibernate.boot.Metadata)8 ArrayList (java.util.ArrayList)7 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)7 List (java.util.List)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 TimeUnit (java.util.concurrent.TimeUnit)5 AnnotationException (org.hibernate.AnnotationException)5 Collections (java.util.Collections)4