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()));
}
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();
}
}
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;
}
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;
}
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;
}
Aggregations