Search in sources :

Example 71 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class PooledHiLoSequenceIdentifierTest method insertNewRow.

private void insertNewRow(Session session) {
    final SessionImplementor si = (SessionImplementor) session;
    final SessionFactoryImplementor sfi = si.getFactory();
    session.doWork(new Work() {

        @Override
        public void execute(Connection connection) throws SQLException {
            PreparedStatement statement = null;
            try {
                statement = connection.prepareStatement("INSERT INTO sequenceIdentifier VALUES (?)");
                statement.setObject(1, sfi.getIdentifierGenerator(SequenceIdentifier.class.getName()).generate(si, null));
                statement.executeUpdate();
            } finally {
                if (statement != null) {
                    statement.close();
                }
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Work(org.hibernate.jdbc.Work) Connection(java.sql.Connection) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PreparedStatement(java.sql.PreparedStatement)

Example 72 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class QueryTranslatorTestCase method assertTranslation.

protected void assertTranslation(String hql, Map replacements, boolean scalar, String sql) {
    SessionFactoryImplementor factory = sessionFactory();
    // Create an empty replacements map if we don't have one.
    if (replacements == null) {
        replacements = new HashMap();
    }
    // steve -> note that the empty maps here represent the currently enabled filters...
    QueryTranslator oldQueryTranslator = null;
    Exception oldException = null;
    try {
        System.out.println("Compiling with classic QueryTranslator...");
        QueryTranslatorFactory classic = new ClassicQueryTranslatorFactory();
        oldQueryTranslator = classic.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory, null);
        oldQueryTranslator.compile(replacements, scalar);
    } catch (QueryException e) {
        oldException = e;
    } catch (MappingException e) {
        oldException = e;
    }
    QueryTranslator newQueryTranslator = null;
    Exception newException = null;
    try {
        System.out.println("Compiling with AST QueryTranslator...");
        newQueryTranslator = createNewQueryTranslator(hql, replacements, scalar);
    } catch (QueryException e) {
        newException = e;
    } catch (MappingException e) {
        newException = e;
    }
    // If the old QT threw an exception, the new one should too.
    if (oldException != null) {
        assertNotNull("New query translator did *NOT* throw an exception, the old one did : " + oldException, newException);
        assertEquals(oldException.getMessage(), newException.getMessage());
        // Don't bother with the rest of the assertions.
        return;
    } else if (newException != null) {
        newException.printStackTrace();
        assertNull("Old query translator did not throw an exception, the new one did", newException);
    }
    // -- check all of the outputs --
    checkSql(oldQueryTranslator, newQueryTranslator, hql, scalar, sql);
    checkQuerySpaces(oldQueryTranslator, newQueryTranslator);
    checkReturnedTypes(oldQueryTranslator, newQueryTranslator);
}
Also used : ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) QueryException(org.hibernate.QueryException) HashMap(java.util.HashMap) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) MappingException(org.hibernate.MappingException) QueryException(org.hibernate.QueryException) MappingException(org.hibernate.MappingException)

Example 73 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class QueryTranslatorTestCase method compileBadHql.

protected Exception compileBadHql(String hql, boolean scalar) {
    QueryTranslator newQueryTranslator;
    Map replacements = null;
    Exception newException = null;
    SessionFactoryImplementor factory = sessionFactory();
    try {
        QueryTranslatorFactory ast = new ASTQueryTranslatorFactory();
        newQueryTranslator = ast.createQueryTranslator(hql, hql, Collections.EMPTY_MAP, factory, null);
        newQueryTranslator.compile(replacements, scalar);
    } catch (QueryException e) {
        newException = e;
    } catch (MappingException e) {
        newException = e;
    }
    assertNotNull("Expected exception from compilation of '" + hql + "'!", newException);
    return newException;
}
Also used : ClassicQueryTranslatorFactory(org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) QueryTranslatorFactory(org.hibernate.hql.spi.QueryTranslatorFactory) QueryException(org.hibernate.QueryException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryTranslator(org.hibernate.hql.spi.QueryTranslator) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) MappingException(org.hibernate.MappingException) QueryException(org.hibernate.QueryException) ASTQueryTranslatorFactory(org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory) MappingException(org.hibernate.MappingException)

Example 74 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class LoadPlanStructureAssertionTest method testEncapsulatedCompositeIdNoFetches2.

@Test
public void testEncapsulatedCompositeIdNoFetches2() {
    // Parent is an entity with a composite identifier mapped via a @EmbeddedId class (ParentPK) which is defined
    // using just basic types (strings, ints, etc)
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(EncapsulatedCompositeIdResultSetProcessorTest.Parent.class);
    SessionFactoryImplementor sf = (SessionFactoryImplementor) cfg.buildSessionFactory();
    try {
        doCompare(sf, (OuterJoinLoadable) sf.getClassMetadata(EncapsulatedCompositeIdResultSetProcessorTest.Parent.class));
    } finally {
        sf.close();
    }
}
Also used : Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) EncapsulatedCompositeIdResultSetProcessorTest(org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest) Test(org.junit.Test) EncapsulatedCompositeIdResultSetProcessorTest(org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)

Example 75 with SessionFactoryImplementor

use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.

the class LoadPlanStructureAssertionTest method testEncapsulatedCompositeIdWithFetches1.

@Test
public void testEncapsulatedCompositeIdWithFetches1() {
    Configuration cfg = new Configuration();
    cfg.addAnnotatedClass(Card.class);
    cfg.addAnnotatedClass(CardField.class);
    cfg.addAnnotatedClass(Key.class);
    cfg.addAnnotatedClass(PrimaryKey.class);
    SessionFactoryImplementor sf = (SessionFactoryImplementor) cfg.buildSessionFactory();
    try {
        final OuterJoinLoadable cardFieldPersister = (OuterJoinLoadable) sf.getClassMetadata(CardField.class);
        doCompare(sf, cardFieldPersister);
        final LoadPlan loadPlan = LoadPlanStructureAssertionHelper.INSTANCE.buildLoadPlan(sf, cardFieldPersister);
        assertEquals(LoadPlan.Disposition.ENTITY_LOADER, loadPlan.getDisposition());
        assertEquals(1, loadPlan.getReturns().size());
        final EntityReturn cardFieldReturn = assertTyping(EntityReturn.class, loadPlan.getReturns().get(0));
        assertEquals(0, cardFieldReturn.getFetches().length);
        // CardField defines a composite pk with 2 many-to-ones : Card and Key (the id description acts as the composite);
        // because it is an @EmbeddedId, the ID provided by the application is used "as is"
        // and fetches are not included in the load plan.
        assertFalse(cardFieldReturn.getIdentifierDescription().hasFetches());
    // we need the readers ordered in a certain manner.  Here specifically: Fetch(Card), Fetch(Key), Return(CardField)
    //
    // additionally, we need Fetch(Card) and Fetch(Key) to be hydrated/semi-resolved beforeQuery attempting to
    // resolve the EntityKey for Return(CardField)
    //
    // together those sound like argument enough to continue keeping readers for "identifier fetches" as part of
    // a special "identifier reader".  generated aliases could help here too to remove cyclic-ness from the graph.
    // but at any rate, we need to know still when this becomes circularity
    } finally {
        sf.close();
    }
}
Also used : OuterJoinLoadable(org.hibernate.persister.entity.OuterJoinLoadable) Configuration(org.hibernate.cfg.Configuration) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) CardField(org.hibernate.test.annotations.cid.keymanytoone.CardField) EntityReturn(org.hibernate.loader.plan.spi.EntityReturn) Test(org.junit.Test) EncapsulatedCompositeIdResultSetProcessorTest(org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)

Aggregations

SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)129 Test (org.junit.Test)46 Configuration (org.hibernate.cfg.Configuration)27 SQLException (java.sql.SQLException)18 Type (org.hibernate.type.Type)16 EntityPersister (org.hibernate.persister.entity.EntityPersister)13 Connection (java.sql.Connection)12 HibernateException (org.hibernate.HibernateException)12 PreparedStatement (java.sql.PreparedStatement)11 Metadata (org.hibernate.boot.Metadata)11 Statement (java.sql.Statement)10 ArrayList (java.util.ArrayList)10 JdbcConnectionAccess (org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess)10 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)10 Serializable (java.io.Serializable)9 EncapsulatedCompositeIdResultSetProcessorTest (org.hibernate.test.loadplans.process.EncapsulatedCompositeIdResultSetProcessorTest)8 HashMap (java.util.HashMap)7 Integrator (org.hibernate.integrator.spi.Integrator)7 SessionFactoryServiceRegistry (org.hibernate.service.spi.SessionFactoryServiceRegistry)7 Map (java.util.Map)6