Search in sources :

Example 16 with SessionFactory

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

the class TransactionsTest method jdbc.

@Test
public void jdbc() {
    //tag::transactions-api-jdbc-example[]
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySetting(AvailableSettings.TRANSACTION_COORDINATOR_STRATEGY, "jdbc").build();
    Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Customer.class).getMetadataBuilder().build();
    SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build();
    Session session = sessionFactory.openSession();
    try {
        // calls Connection#setAutoCommit( false ) to
        // signal start of transaction
        session.getTransaction().begin();
        session.createQuery("UPDATE customer set NAME = 'Sir. '||NAME").executeUpdate();
        // calls Connection#commit(), if an error
        // happens we attempt a rollback
        session.getTransaction().commit();
    } catch (Exception e) {
        // where the exception happened
        if (session.getTransaction().getStatus() == TransactionStatus.ACTIVE || session.getTransaction().getStatus() == TransactionStatus.MARKED_ROLLBACK) {
            session.getTransaction().rollback();
        }
    // handle the underlying error
    } finally {
        session.close();
        sessionFactory.close();
    }
//end::transactions-api-jdbc-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 17 with SessionFactory

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

the class ManagedSessionContext method unbind.

/**
	 * Unbinds the session (if one) current associated with the context for the
	 * given session.
	 *
	 * @param factory The factory for which to unbind the current session.
	 * @return The bound session if one, else null.
	 */
public static Session unbind(SessionFactory factory) {
    final Map<SessionFactory, Session> sessionMap = sessionMap();
    Session existing = null;
    if (sessionMap != null) {
        existing = sessionMap.remove(factory);
        doCleanup();
    }
    return existing;
}
Also used : SessionFactory(org.hibernate.SessionFactory) Session(org.hibernate.Session)

Example 18 with SessionFactory

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

the class SessionFactoryRegistry method clearRegistrations.

public void clearRegistrations() {
    nameUuidXref.clear();
    for (SessionFactory factory : sessionFactoryMap.values()) {
        try {
            factory.close();
        } catch (Exception ignore) {
        }
    }
    sessionFactoryMap.clear();
}
Also used : SessionFactory(org.hibernate.SessionFactory) JndiException(org.hibernate.engine.jndi.JndiException) JndiNameException(org.hibernate.engine.jndi.JndiNameException)

Example 19 with SessionFactory

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

the class EntityManagerFactoryUnwrapTest method testEntityManagerCanBeUnwrappedToSessionFactory.

@Test
public void testEntityManagerCanBeUnwrappedToSessionFactory() {
    SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class);
    assertNotNull("Unwrapping to API class SessionFactory should be ok", sessionFactory);
}
Also used : SessionFactory(org.hibernate.SessionFactory) Test(org.junit.Test)

Example 20 with SessionFactory

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

the class ConfigurationTest method testIgnoringHbm.

@Test
public void testIgnoringHbm() throws Exception {
    Configuration cfg = new Configuration();
    cfg.configure("org/hibernate/test/annotations/hibernate.cfg.xml");
    cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
    cfg.setProperty(Configuration.ARTEFACT_PROCESSING_ORDER, "class");
    SessionFactory sf = cfg.buildSessionFactory();
    assertNotNull(sf);
    Session s = sf.openSession();
    Transaction tx = s.beginTransaction();
    Query q;
    try {
        s.createQuery("from Boat").list();
        fail("Boat should not be mapped");
    } catch (IllegalArgumentException e) {
        assertTyping(QuerySyntaxException.class, e.getCause());
    //all good
    }
    q = s.createQuery("from Plane");
    assertEquals(0, q.list().size());
    tx.commit();
    s.close();
    sf.close();
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) QuerySyntaxException(org.hibernate.hql.internal.ast.QuerySyntaxException) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

SessionFactory (org.hibernate.SessionFactory)106 Test (org.junit.Test)62 Session (org.hibernate.Session)49 Configuration (org.hibernate.cfg.Configuration)34 Transaction (org.hibernate.Transaction)19 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