Search in sources :

Example 96 with SessionFactory

use of org.hibernate.SessionFactory in project wildfly by wildfly.

the class SessionFactoryTestCase method testHibernateSessionFactoryName.

// test that we didn't break the Hibernate hibernate.session_factory_name (bind Hibernate session factory to
// specified jndi name) functionality.
@Test
public void testHibernateSessionFactoryName() throws Exception {
    SFSB1 sfsb1 = lookup("SFSB1", SFSB1.class);
    sfsb1.createEmployee("Sally", "1 home street", 1);
    // check if we can look up the Hibernate session factory that should of been bound because of
    // the hibernate.session_factory_name was specified in the properties (in peristence.xml above).
    SessionFactory hibernateSessionFactory = rawLookup("modelSessionFactory", SessionFactory.class);
    assertNotNull("jndi lookup of hibernate.session_factory_name should return HibernateSessionFactory", hibernateSessionFactory);
    Session session = hibernateSessionFactory.openSession();
    Employee emp = session.get(Employee.class, 1);
    assertTrue("name read from hibernate session is Sally", "Sally".equals(emp.getName()));
}
Also used : SFSBHibernateSessionFactory(org.jboss.as.test.integration.jpa.hibernate.SFSBHibernateSessionFactory) SessionFactory(org.hibernate.SessionFactory) Employee(org.jboss.as.test.integration.jpa.hibernate.Employee) SFSB1(org.jboss.as.test.integration.jpa.hibernate.SFSB1) Session(org.hibernate.Session) SFSBHibernateSession(org.jboss.as.test.integration.jpa.hibernate.SFSBHibernateSession) Test(org.junit.Test)

Example 97 with SessionFactory

use of org.hibernate.SessionFactory in project intellij-community by JetBrains.

the class HibernateResourceInspection method foo5.

public void foo5() {
    SessionFactory factory = null;
    Session session = null;
    try {
        session = factory.openSession();
    } finally {
        session.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) Session(org.hibernate.classic.Session)

Example 98 with SessionFactory

use of org.hibernate.SessionFactory in project midpoint by Evolveum.

the class TestSqlRepositoryBeanPostProcessor method postProcessAfterInitialization.

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if ((bean instanceof SqlRepositoryFactory) || (bean instanceof SessionFactory) || (bean instanceof SqlRepositoryServiceImpl)) {
        LOGGER.info("Post process: " + bean.getClass().getName());
    }
    if (!(bean instanceof SessionFactory)) {
        return bean;
    }
    LOGGER.info("Postprocessing session factory - removing everything from database if necessary.");
    TestSqlRepositoryFactory factory = context.getBean(TestSqlRepositoryFactory.class);
    //we'll attempt to drop database objects if configuration contains dropIfExists=true and embedded=false
    SqlRepositoryConfiguration config = factory.getSqlConfiguration();
    if (!config.isDropIfExists() || config.isEmbedded()) {
        LOGGER.info("We're not deleting objects from DB, drop if exists=false or embedded=true.");
        return bean;
    }
    LOGGER.info("Deleting objects from database.");
    SessionFactory sessionFactory = (SessionFactory) bean;
    Session session = sessionFactory.openSession();
    try {
        session.beginTransaction();
        Query query;
        if (useProcedure(factory.getSqlConfiguration())) {
            LOGGER.info("Using truncate procedure.");
            query = session.createSQLQuery("{ call " + TRUNCATE_PROCEDURE + "() }");
            query.executeUpdate();
        } else {
            LOGGER.info("Using truncate function.");
            query = session.createSQLQuery("select " + TRUNCATE_FUNCTION + "();");
            query.uniqueResult();
        }
        session.getTransaction().commit();
    } catch (Exception ex) {
        LOGGER.error("Couldn't cleanup database, reason: " + ex.getMessage(), ex);
        if (session != null && session.isOpen()) {
            Transaction transaction = session.getTransaction();
            if (transaction != null && transaction.isActive()) {
                transaction.rollback();
            }
        }
        throw new BeanInitializationException("Couldn't cleanup database, reason: " + ex.getMessage(), ex);
    } finally {
        if (session != null && session.isOpen()) {
            session.close();
        }
    }
    return bean;
}
Also used : SessionFactory(org.hibernate.SessionFactory) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) Query(org.hibernate.Query) Transaction(org.hibernate.Transaction) SqlRepositoryFactory(com.evolveum.midpoint.repo.sql.SqlRepositoryFactory) SqlRepositoryServiceImpl(com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl) BeansException(org.springframework.beans.BeansException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) SqlRepositoryConfiguration(com.evolveum.midpoint.repo.sql.SqlRepositoryConfiguration) Session(org.hibernate.Session)

Example 99 with SessionFactory

use of org.hibernate.SessionFactory in project wildfly by wildfly.

the class HibernateEntityCacheStatistics method getStatistics.

org.hibernate.stat.SecondLevelCacheStatistics getStatistics(EntityManagerFactoryAccess entityManagerFactoryaccess, PathAddress pathAddress) {
    String scopedPersistenceUnitName = pathAddress.getValue(HibernateStatistics.PROVIDER_LABEL);
    HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactoryaccess.entityManagerFactory(scopedPersistenceUnitName);
    if (entityManagerFactoryImpl == null) {
        return null;
    }
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    if (sessionFactory != null) {
        return sessionFactory.getStatistics().getSecondLevelCacheStatistics(scopedPersistenceUnitName + "." + pathAddress.getValue(HibernateStatistics.ENTITYCACHE));
    }
    return null;
}
Also used : SessionFactory(org.hibernate.SessionFactory) HibernateEntityManagerFactory(org.hibernate.jpa.HibernateEntityManagerFactory)

Example 100 with SessionFactory

use of org.hibernate.SessionFactory in project wildfly by wildfly.

the class HibernateEntityCacheStatistics method getBaseStatistics.

private org.hibernate.stat.Statistics getBaseStatistics(EntityManagerFactory entityManagerFactory) {
    if (entityManagerFactory == null) {
        return null;
    }
    HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    if (sessionFactory != null) {
        return sessionFactory.getStatistics();
    }
    return null;
}
Also used : SessionFactory(org.hibernate.SessionFactory) HibernateEntityManagerFactory(org.hibernate.jpa.HibernateEntityManagerFactory)

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