use of org.hibernate.SessionFactory in project intellij-community by JetBrains.
the class HibernateResourceInspection method foo.
public void foo() {
final SessionFactory factory = null;
factory.openSession();
}
use of org.hibernate.SessionFactory in project intellij-community by JetBrains.
the class HibernateResourceInspection method foo3.
public void foo3() {
final SessionFactory factory = null;
final Session session = factory.openSession();
session.close();
}
use of org.hibernate.SessionFactory in project midpoint by Evolveum.
the class OrgClosureManager method autoUpdateClosureTableStructure.
// TEMPORARY and quite BRUTAL HACK
// Originally in midPoint 3.0, m_org_closure has 5 columns, a non-null ID among them.
// In 3.1, it has 3, and no ID. Unfortunately, hibernate's hbm2ddl tool does not automatically remove ID column,
// so people can expect quite hard-to-understand error messages when running midPoint after upgrade.
//
// This code removes and re-creates the m_org_closure table if hbm2ddl is set to "update".
//
// returns true if the table was re-created
private boolean autoUpdateClosureTableStructure() {
if (baseHelper.getConfiguration().isSkipOrgClosureStructureCheck()) {
LOGGER.debug("Skipping org closure structure check.");
return false;
}
SessionFactory sf = baseHelper.getSessionFactory();
if (sf instanceof SessionFactoryImpl) {
SessionFactoryImpl sfi = ((SessionFactoryImpl) sf);
LOGGER.debug("SessionFactoryImpl.getSettings() = {}; auto update schema = {}", sfi.getSettings(), sfi.getSettings() != null ? sfi.getSettings().isAutoUpdateSchema() : null);
if (sfi.getSettings() != null && sfi.getSettings().isAutoUpdateSchema()) {
LOGGER.info("Checking the closure table structure.");
final Session session = baseHelper.getSessionFactory().openSession();
final Holder<Boolean> wrongNumberOfColumns = new Holder<>(false);
session.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
DatabaseMetaData meta = connection.getMetaData();
if (meta == null) {
LOGGER.warn("No database metadata found.");
} else {
ResultSet rsColumns = meta.getColumns(null, null, CLOSURE_TABLE_NAME, null);
int columns = 0;
while (rsColumns.next()) {
LOGGER.debug("Column: {} {}", rsColumns.getString("TYPE_NAME"), rsColumns.getString("COLUMN_NAME"));
columns++;
}
if (columns > 0) {
LOGGER.debug("There are {} columns in {} (obtained via DatabaseMetaData)", columns, CLOSURE_TABLE_NAME);
if (columns != 3) {
wrongNumberOfColumns.setValue(true);
}
return;
}
// perhaps some problem here... let's try another way out
try {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select * from " + CLOSURE_TABLE_NAME);
int cols = rs.getMetaData().getColumnCount();
if (cols > 0) {
LOGGER.debug("There are {} columns in {} (obtained via resultSet.getMetaData())", cols, CLOSURE_TABLE_NAME);
if (cols != 3) {
wrongNumberOfColumns.setValue(true);
}
} else {
LOGGER.warn("Couldn't determine the number of columns in {}. In case of problems, please fix your database structure manually using DB scripts in 'config' folder.", CLOSURE_TABLE_NAME);
}
// don't care about closing them in case of failure
rs.close();
stmt.close();
} catch (RuntimeException e) {
LoggingUtils.logException(LOGGER, "Couldn't obtain the number of columns in {}. In case of problems running midPoint, please fix your database structure manually using DB scripts in 'config' folder.", e, CLOSURE_TABLE_NAME);
}
}
}
});
if (wrongNumberOfColumns.getValue()) {
session.getTransaction().begin();
LOGGER.info("Wrong number of columns detected; dropping table " + CLOSURE_TABLE_NAME);
Query q = session.createSQLQuery("drop table " + CLOSURE_TABLE_NAME);
q.executeUpdate();
session.getTransaction().commit();
LOGGER.info("Calling hibernate hbm2ddl SchemaUpdate tool to create the table in the necessary form.");
new SchemaUpdate(sfi.getServiceRegistry(), baseHelper.getSessionFactoryBean().getConfiguration()).execute(false, true);
LOGGER.info("Done, table was (hopefully) created. If not, please fix your database structure manually using DB scripts in 'config' folder.");
return true;
}
} else {
// auto schema update is disabled
}
} else {
LOGGER.warn("SessionFactory is not of type SessionFactoryImpl; it is {}", sf != null ? sf.getClass() : "null");
}
return false;
}
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);
SessionFactory sessionFactory = entityManagerFactoryaccess.entityManagerFactory(scopedPersistenceUnitName).unwrap(SessionFactory.class);
if (sessionFactory != null) {
return sessionFactory.getStatistics().getSecondLevelCacheStatistics(scopedPersistenceUnitName + "." + pathAddress.getValue(HibernateStatistics.ENTITYCACHE));
}
return null;
}
use of org.hibernate.SessionFactory in project spring-framework by spring-projects.
the class HibernateJpaSessionFactoryBean method getObject.
@Override
public SessionFactory getObject() {
EntityManagerFactory emf = getEntityManagerFactory();
Assert.state(emf != null, "EntityManagerFactory must not be null");
try {
Method getSessionFactory = emf.getClass().getMethod("getSessionFactory");
return (SessionFactory) ReflectionUtils.invokeMethod(getSessionFactory, emf);
} catch (NoSuchMethodException ex) {
throw new IllegalStateException("No compatible Hibernate EntityManagerFactory found: " + ex);
}
}
Aggregations