Search in sources :

Example 6 with SessionFactoryImplementor

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;
}
Also used : SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.SessionFactoryImplementor) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) ConnectionProvider(org.hibernate.connection.ConnectionProvider)

Example 7 with SessionFactoryImplementor

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();
}
Also used : SessionFactoryImplementor(org.hibernate.engine.SessionFactoryImplementor) Dialect(org.hibernate.dialect.Dialect) SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with SessionFactoryImplementor

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();
                }
            }
        }
    }
}
Also used : HibernateException(org.hibernate.HibernateException) SessionFactoryImplementor(org.hibernate.engine.SessionFactoryImplementor) Dialect(org.hibernate.dialect.Dialect)

Example 9 with SessionFactoryImplementor

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);
}
Also used : RootClass(org.hibernate.mapping.RootClass) Table(org.hibernate.mapping.Table) Configuration(org.hibernate.cfg.Configuration) Column(org.hibernate.mapping.Column) SessionFactoryImplementor(org.hibernate.engine.SessionFactoryImplementor) ArrayList(java.util.ArrayList) SessionFactoryImpl(org.hibernate.impl.SessionFactoryImpl) SimpleValue(org.hibernate.mapping.SimpleValue)

Example 10 with SessionFactoryImplementor

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());
}
Also used : RootClass(org.hibernate.mapping.RootClass) HQLQueryPlan(org.hibernate.engine.query.HQLQueryPlan) IHQLQueryPlan(org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan) ITable(org.jboss.tools.hibernate.runtime.spi.ITable) Table(org.hibernate.mapping.Table) Configuration(org.hibernate.cfg.Configuration) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) Mappings(org.hibernate.cfg.Mappings) Column(org.hibernate.mapping.Column) IColumn(org.jboss.tools.hibernate.runtime.spi.IColumn) SessionFactoryImplementor(org.hibernate.engine.SessionFactoryImplementor) PrimaryKey(org.hibernate.mapping.PrimaryKey) IPrimaryKey(org.jboss.tools.hibernate.runtime.spi.IPrimaryKey) SessionFactoryImpl(org.hibernate.impl.SessionFactoryImpl) IHQLQueryPlan(org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan) SimpleValue(org.hibernate.mapping.SimpleValue) Test(org.junit.Test)

Aggregations

SessionFactoryImplementor (org.hibernate.engine.SessionFactoryImplementor)13 Configuration (org.hibernate.cfg.Configuration)8 SessionFactoryImpl (org.hibernate.impl.SessionFactoryImpl)8 RootClass (org.hibernate.mapping.RootClass)8 SimpleValue (org.hibernate.mapping.SimpleValue)8 Column (org.hibernate.mapping.Column)6 Table (org.hibernate.mapping.Table)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 EntityMetamodel (org.hibernate.tuple.entity.EntityMetamodel)4 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)4 IEntityMetamodel (org.jboss.tools.hibernate.runtime.spi.IEntityMetamodel)4 Session (org.hibernate.Session)3 Mappings (org.hibernate.cfg.Mappings)3 Dialect (org.hibernate.dialect.Dialect)3 EntityMode (org.hibernate.EntityMode)2 SQLQuery (org.hibernate.SQLQuery)2 ConnectionProvider (org.hibernate.connection.ConnectionProvider)2 HQLQueryPlan (org.hibernate.engine.query.HQLQueryPlan)2 PrimaryKey (org.hibernate.mapping.PrimaryKey)2