Search in sources :

Example 21 with QueryParameters

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

the class DynamicBatchingEntityLoaderBuilder method performOrderedBatchLoad.

private void performOrderedBatchLoad(List<Serializable> idsInBatch, LockOptions lockOptions, OuterJoinLoadable persister, SharedSessionContractImplementor session) {
    final int batchSize = idsInBatch.size();
    final DynamicEntityLoader batchingLoader = new DynamicEntityLoader(persister, batchSize, lockOptions, session.getFactory(), session.getLoadQueryInfluencers());
    final Serializable[] idsInBatchArray = idsInBatch.toArray(new Serializable[idsInBatch.size()]);
    QueryParameters qp = buildMultiLoadQueryParameters(persister, idsInBatchArray, lockOptions);
    batchingLoader.doEntityBatchFetch(session, qp, idsInBatchArray);
    idsInBatch.clear();
}
Also used : Serializable(java.io.Serializable) QueryParameters(org.hibernate.engine.spi.QueryParameters)

Example 22 with QueryParameters

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

the class DynamicBatchingEntityLoaderBuilder method buildMultiLoadQueryParameters.

public static QueryParameters buildMultiLoadQueryParameters(OuterJoinLoadable persister, Serializable[] ids, LockOptions lockOptions) {
    Type[] types = new Type[ids.length];
    Arrays.fill(types, persister.getIdentifierType());
    QueryParameters qp = new QueryParameters();
    qp.setOptionalEntityName(persister.getEntityName());
    qp.setPositionalParameterTypes(types);
    qp.setPositionalParameterValues(ids);
    qp.setLockOptions(lockOptions);
    qp.setOptionalObject(null);
    qp.setOptionalId(null);
    return qp;
}
Also used : Type(org.hibernate.type.Type) QueryParameters(org.hibernate.engine.spi.QueryParameters)

Example 23 with QueryParameters

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

the class AbstractLoadPlanBasedEntityLoader method loadEntityBatch.

/**
	 * Called by wrappers that batch load entities
	 * @param persister only needed for logging
	 * @param lockOptions
	 */
public final List loadEntityBatch(final SharedSessionContractImplementor session, final Serializable[] ids, final Type idType, final Object optionalObject, final String optionalEntityName, final Serializable optionalId, final EntityPersister persister, LockOptions lockOptions) throws HibernateException {
    if (log.isDebugEnabled()) {
        log.debugf("Batch loading entity: %s", MessageHelper.infoString(persister, ids, getFactory()));
    }
    final Type[] types = new Type[ids.length];
    Arrays.fill(types, idType);
    List result;
    try {
        final QueryParameters qp = new QueryParameters();
        qp.setPositionalParameterTypes(types);
        qp.setPositionalParameterValues(ids);
        qp.setLockOptions(lockOptions);
        result = executeLoad(session, qp, staticLoadQuery, false, null);
    } catch (SQLException sqle) {
        throw getFactory().getSQLExceptionHelper().convert(sqle, "could not load an entity batch: " + MessageHelper.infoString(entityPersister, ids, getFactory()), staticLoadQuery.getSqlStatement());
    }
    log.debug("Done entity batch load");
    return result;
}
Also used : Type(org.hibernate.type.Type) SQLException(java.sql.SQLException) List(java.util.List) QueryParameters(org.hibernate.engine.spi.QueryParameters)

Example 24 with QueryParameters

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

the class EncapsulatedCompositeIdResultSetProcessorTest method testCompositeIdWithKeyManyToOne.

@Test
public void testCompositeIdWithKeyManyToOne() throws Exception {
    final String cardId = "ace-of-spades";
    // create some test data
    Session session = openSession();
    session.beginTransaction();
    Card card = new Card(cardId);
    final CardField cardField = new CardField(card, 1);
    session.persist(card);
    session.persist(cardField);
    session.getTransaction().commit();
    session.close();
    session = openSession();
    session.beginTransaction();
    Card cardProxy = (Card) session.load(Card.class, cardId);
    final CardFieldPK cardFieldPK = new CardFieldPK(cardProxy, 1);
    CardField cardFieldGotten = (CardField) session.get(CardField.class, cardFieldPK);
    //assertEquals( card, cardGotten );
    session.getTransaction().commit();
    session.close();
    final EntityPersister entityPersister = sessionFactory().getEntityPersister(CardField.class.getName());
    final List results = getResults(entityPersister, new Callback() {

        @Override
        public void bind(PreparedStatement ps) throws SQLException {
            ps.setString(1, cardField.primaryKey.card.id);
            ps.setInt(2, cardField.primaryKey.fieldNumber);
        }

        @Override
        public QueryParameters getQueryParameters() {
            QueryParameters qp = new QueryParameters();
            qp.setPositionalParameterTypes(new Type[] { entityPersister.getIdentifierType() });
            qp.setPositionalParameterValues(new Object[] { cardFieldPK });
            qp.setOptionalObject(null);
            qp.setOptionalEntityName(entityPersister.getEntityName());
            qp.setOptionalId(cardFieldPK);
            qp.setLockOptions(LockOptions.NONE);
            return qp;
        }
    });
    assertEquals(1, results.size());
    Object result = results.get(0);
    assertNotNull(result);
    CardField cardFieldWork = ExtraAssertions.assertTyping(CardField.class, result);
    assertEquals(cardFieldGotten, cardFieldWork);
    // clean up test data
    session = openSession();
    session.beginTransaction();
    session.createQuery("delete CardField").executeUpdate();
    session.createQuery("delete Card").executeUpdate();
    session.getTransaction().commit();
    session.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) QueryParameters(org.hibernate.engine.spi.QueryParameters) Type(org.hibernate.type.Type) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 25 with QueryParameters

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

the class EntityAssociationResultSetProcessorTest method testNestedManyToOneEntityProcessing.

@Test
public void testNestedManyToOneEntityProcessing() throws Exception {
    final EntityPersister entityPersister = sessionFactory().getEntityPersister(ReportedMessage.class.getName());
    // create some test data
    Session session = openSession();
    session.beginTransaction();
    Message message = new Message(1, "the message");
    Poster poster = new Poster(2, "the poster");
    session.save(message);
    session.save(poster);
    message.poster = poster;
    poster.messages.add(message);
    ReportedMessage reportedMessage = new ReportedMessage(0, "inappropriate", message);
    session.save(reportedMessage);
    session.getTransaction().commit();
    session.close();
    {
        final LoadPlan plan = Helper.INSTANCE.buildLoadPlan(sessionFactory(), entityPersister);
        final LoadQueryDetails queryDetails = Helper.INSTANCE.buildLoadQueryDetails(plan, sessionFactory());
        final String sql = queryDetails.getSqlStatement();
        final ResultSetProcessor resultSetProcessor = queryDetails.getResultSetProcessor();
        final List results = new ArrayList();
        final Session workSession = openSession();
        workSession.beginTransaction();
        workSession.doWork(new Work() {

            @Override
            public void execute(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setInt(1, 0);
                ResultSet resultSet = ps.executeQuery();
                results.addAll(resultSetProcessor.extractResults(resultSet, (SessionImplementor) workSession, new QueryParameters(), new NamedParameterContext() {

                    @Override
                    public int[] getNamedParameterLocations(String name) {
                        return new int[0];
                    }
                }, true, false, null, null));
                resultSet.close();
                ps.close();
            }
        });
        assertEquals(1, results.size());
        Object result = results.get(0);
        assertNotNull(result);
        ReportedMessage workReportedMessage = ExtraAssertions.assertTyping(ReportedMessage.class, result);
        assertEquals(0, workReportedMessage.id.intValue());
        assertEquals("inappropriate", workReportedMessage.reason);
        Message workMessage = workReportedMessage.message;
        assertNotNull(workMessage);
        assertTrue(Hibernate.isInitialized(workMessage));
        assertEquals(1, workMessage.mid.intValue());
        assertEquals("the message", workMessage.msgTxt);
        assertTrue(Hibernate.isInitialized(workMessage.poster));
        Poster workPoster = workMessage.poster;
        assertEquals(2, workPoster.pid.intValue());
        assertEquals("the poster", workPoster.name);
        assertFalse(Hibernate.isInitialized(workPoster.messages));
        workSession.getTransaction().commit();
        workSession.close();
    }
    // clean up test data
    session = openSession();
    session.beginTransaction();
    session.createQuery("delete ReportedMessage").executeUpdate();
    session.createQuery("delete Message").executeUpdate();
    session.createQuery("delete Poster").executeUpdate();
    session.getTransaction().commit();
    session.close();
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NamedParameterContext(org.hibernate.loader.plan.exec.query.spi.NamedParameterContext) ResultSetProcessor(org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) QueryParameters(org.hibernate.engine.spi.QueryParameters) LoadPlan(org.hibernate.loader.plan.spi.LoadPlan) LoadQueryDetails(org.hibernate.loader.plan.exec.spi.LoadQueryDetails) Work(org.hibernate.jdbc.Work) ResultSet(java.sql.ResultSet) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

QueryParameters (org.hibernate.engine.spi.QueryParameters)35 List (java.util.List)20 ArrayList (java.util.ArrayList)17 SQLException (java.sql.SQLException)14 PreparedStatement (java.sql.PreparedStatement)10 Session (org.hibernate.Session)10 Type (org.hibernate.type.Type)10 Test (org.junit.Test)9 EntityPersister (org.hibernate.persister.entity.EntityPersister)8 Connection (java.sql.Connection)7 ResultSet (java.sql.ResultSet)7 Work (org.hibernate.jdbc.Work)7 ResultSetProcessor (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor)7 NamedParameterContext (org.hibernate.loader.plan.exec.query.spi.NamedParameterContext)7 LoadQueryDetails (org.hibernate.loader.plan.exec.spi.LoadQueryDetails)7 LoadPlan (org.hibernate.loader.plan.spi.LoadPlan)7 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)6 AssociationType (org.hibernate.type.AssociationType)5 Serializable (java.io.Serializable)4 EntityType (org.hibernate.type.EntityType)4