use of org.hibernate.engine.SessionFactoryImplementor in project xwiki-platform by xwiki.
the class HibernateStore method getDatabaseMetaData.
/**
* Retrieve metadata about the database used (name, version, etc).
* <p>
* Note that the database metadata is not cached and it's retrieved at each call. If all you need is the database
* product name you should use {@link #getDatabaseProductName()} instead, which is cached.
* </p>
*
* @return the database meta data or null if an error occurred
* @since 6.1M1
*/
public DatabaseMetaData getDatabaseMetaData() {
DatabaseMetaData result;
Connection connection = null;
// Note that we need to do the cast because this is how Hibernate suggests to get the Connection Provider.
// See http://bit.ly/QAJXlr
ConnectionProvider connectionProvider = ((SessionFactoryImplementor) getSessionFactory()).getConnectionProvider();
try {
connection = connectionProvider.getConnection();
result = connection.getMetaData();
} catch (SQLException ignored) {
result = null;
} finally {
if (connection != null) {
try {
connectionProvider.closeConnection(connection);
} catch (SQLException ignored) {
// Ignore
}
}
}
return result;
}
use of org.hibernate.engine.SessionFactoryImplementor in project xwiki-platform by xwiki.
the class XWikiHibernateStoreTest method createHibernateSequenceIfRequiredWhenNotInUpdateCommands.
@Test
public void createHibernateSequenceIfRequiredWhenNotInUpdateCommands() throws Exception {
Session session = mock(Session.class);
SessionFactoryImplementor sessionFactory = mock(SessionFactoryImplementor.class);
Dialect dialect = mock(Dialect.class);
when(session.getSessionFactory()).thenReturn(sessionFactory);
when(sessionFactory.getDialect()).thenReturn(dialect);
when(dialect.getNativeIdentifierGeneratorClass()).thenReturn(SequenceGenerator.class);
SQLQuery sqlQuery = mock(SQLQuery.class);
when(session.createSQLQuery("create sequence schema.hibernate_sequence")).thenReturn(sqlQuery);
when(sqlQuery.executeUpdate()).thenReturn(0);
this.store.createHibernateSequenceIfRequired(new String[] {}, "schema", session);
verify(session).createSQLQuery("create sequence schema.hibernate_sequence");
verify(sqlQuery).executeUpdate();
}
use of org.hibernate.engine.SessionFactoryImplementor in project xwiki-platform by xwiki.
the class XWikiHibernateBaseStore method createHibernateSequenceIfRequired.
/**
* In the Hibernate mapping file for XWiki we use a "native" generator for some tables (deleted document and deleted
* attachments for example - The reason we use generated ids and not custom computed ones is because we don't need
* to address rows from these tables). For a lot of database the Dialect uses an Identity Generator (when the DB
* supports it). PostgreSQL and Oracle don't support it and Hibernate defaults to a Sequence Generator which uses a
* sequence named "hibernate_sequence" by default. Hibernate will normally create such a sequence automatically when
* updating the schema (see #getSchemaUpdateScript). However the problem is that Hibernate maintains a cache of
* sequence names per catalog and will only generate the sequence creation SQL if the sequence is not in this cache.
* Since the main wiki is updated first the sequence named "hibernate_sequence" will be put in this cache, thus
* preventing subwikis to automatically create sequence with the same name (see also
* https://hibernate.atlassian.net/browse/HHH-1672). As a workaround, we create the required sequence here.
*
* @param schemaSQL the list of SQL commands to execute to update the schema, possibly containing the
* "hibernate_sequence" sequence creation
* @param schemaName the schema name corresponding to the subwiki being updated
* @param session the Hibernate session, used to get the Dialect object
* @since 5.2RC1
*/
protected void createHibernateSequenceIfRequired(String[] schemaSQL, String schemaName, Session session) {
// There's no issue when in database mode, only in schema mode.
if (isInSchemaMode()) {
Dialect dialect = ((SessionFactoryImplementor) session.getSessionFactory()).getDialect();
if (dialect.getNativeIdentifierGeneratorClass().equals(SequenceGenerator.class)) {
// We create the sequence only if it's not already in the SQL to execute as otherwise we would get an
// error that the sequence already exists ("relation "hibernate_sequence" already exists").
boolean hasSequence = false;
String sequenceSQL = String.format("create sequence %s.hibernate_sequence", schemaName);
for (String sql : schemaSQL) {
if (sequenceSQL.equals(sql)) {
hasSequence = true;
break;
}
}
if (!hasSequence) {
// try to create the sequence and if it fails then we consider it already exists.
try {
// Ignore errors in the log during the creation of the sequence since we know it can fail and we
// don't want to show false positives to the user.
this.loggerManager.pushLogListener(null);
session.createSQLQuery(sequenceSQL).executeUpdate();
} catch (HibernateException e) {
// Sequence failed to be created, we assume it already exists and that's why an exception was
// raised!
} finally {
this.loggerManager.popLogListener();
}
}
}
}
}
use of org.hibernate.engine.SessionFactoryImplementor in project jbosstools-hibernate by jbosstools.
the class ClassMetadataFacadeTest method createSampleEntityPersister.
private TestEntityPersister createSampleEntityPersister() {
Configuration configuration = new Configuration();
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
SessionFactoryImplementor sfi = new SessionFactoryImpl(configuration, null, configuration.buildSettings(), null, null);
RootClass rc = new RootClass();
Table t = new Table("foobar");
rc.setTable(t);
Column c = new Column("foo");
t.addColumn(c);
ArrayList<Column> keyList = new ArrayList<>();
keyList.add(c);
t.createUniqueKey(keyList);
SimpleValue sv = new SimpleValue(configuration.createMappings());
sv.setNullValue("null");
sv.setTypeName(Integer.class.getName());
sv.addColumn(c);
rc.setEntityName("foobar");
rc.setIdentifier(sv);
return new TestEntityPersister(rc, sfi);
}
use of org.hibernate.engine.SessionFactoryImplementor in project jbosstools-hibernate by jbosstools.
the class FacadeFactoryTest method testCreateHQLQueryPlan.
@Test
public void testCreateHQLQueryPlan() {
Configuration configuration = new Configuration();
configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
Mappings mappings = configuration.createMappings();
Table t = new Table("FOO");
Column c = new Column("foo");
t.addColumn(c);
PrimaryKey key = new PrimaryKey();
key.addColumn(c);
t.setPrimaryKey(key);
SimpleValue sv = new SimpleValue();
sv.setNullValue("null");
sv.setTypeName(Integer.class.getName());
sv.setTable(t);
sv.addColumn(c);
RootClass rc = new RootClass();
rc.setEntityName("foo");
rc.setIdentifier(sv);
rc.setTable(t);
mappings.addClass(rc);
SessionFactoryImplementor sfi = new SessionFactoryImpl(configuration, null, configuration.buildSettings(), null, null);
HQLQueryPlan hqlQueryPlan = new HQLQueryPlan("from foo", false, Collections.emptyMap(), sfi);
IHQLQueryPlan facade = facadeFactory.createHQLQueryPlan(hqlQueryPlan);
Assert.assertSame(hqlQueryPlan, ((IFacade) facade).getTarget());
}
Aggregations