Search in sources :

Example 6 with AfterLoadAction

use of org.hibernate.loader.spi.AfterLoadAction in project hibernate-orm by hibernate.

the class QueryTranslatorImpl method iterate.

/**
	 * Return the query results as an iterator
	 */
@Override
public Iterator iterate(QueryParameters queryParameters, EventSource session) throws HibernateException {
    boolean stats = session.getFactory().getStatistics().isStatisticsEnabled();
    long startTime = 0;
    if (stats) {
        startTime = System.nanoTime();
    }
    try {
        final List<AfterLoadAction> afterLoadActions = new ArrayList<AfterLoadAction>();
        final SqlStatementWrapper wrapper = executeQueryStatement(queryParameters, false, afterLoadActions, session);
        final ResultSet rs = wrapper.getResultSet();
        final PreparedStatement st = (PreparedStatement) wrapper.getStatement();
        HolderInstantiator hi = HolderInstantiator.createClassicHolderInstantiator(holderConstructor, queryParameters.getResultTransformer());
        Iterator result = new IteratorImpl(rs, st, session, queryParameters.isReadOnly(session), returnTypes, getColumnNames(), hi);
        if (stats) {
            final long endTime = System.nanoTime();
            final long milliseconds = TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS);
            session.getFactory().getStatistics().queryExecuted("HQL: " + queryString, 0, milliseconds);
        }
        return result;
    } catch (SQLException sqle) {
        throw getFactory().getJdbcServices().getSqlExceptionHelper().convert(sqle, "could not execute query using iterate", getSQLString());
    }
}
Also used : HolderInstantiator(org.hibernate.hql.internal.HolderInstantiator) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) AfterLoadAction(org.hibernate.loader.spi.AfterLoadAction) ResultSet(java.sql.ResultSet) Iterator(java.util.Iterator) PreparedStatement(java.sql.PreparedStatement) IteratorImpl(org.hibernate.internal.IteratorImpl)

Example 7 with AfterLoadAction

use of org.hibernate.loader.spi.AfterLoadAction in project hibernate-orm by hibernate.

the class CustomLoader method applyLocks.

@Override
protected String applyLocks(String sql, QueryParameters parameters, Dialect dialect, List<AfterLoadAction> afterLoadActions) throws QueryException {
    final LockOptions lockOptions = parameters.getLockOptions();
    if (lockOptions == null || (lockOptions.getLockMode() == LockMode.NONE && lockOptions.getAliasLockCount() == 0)) {
        return sql;
    }
    // user is request locking, lets see if we can apply locking directly to the SQL...
    // 		some dialects wont allow locking with paging...
    afterLoadActions.add(new AfterLoadAction() {

        private final LockOptions originalLockOptions = lockOptions.makeCopy();

        @Override
        public void afterLoad(SharedSessionContractImplementor session, Object entity, Loadable persister) {
            ((Session) session).buildLockRequest(originalLockOptions).lock(persister.getEntityName(), entity);
        }
    });
    parameters.getLockOptions().setLockMode(LockMode.READ);
    return sql;
}
Also used : Loadable(org.hibernate.persister.entity.Loadable) LockOptions(org.hibernate.LockOptions) AfterLoadAction(org.hibernate.loader.spi.AfterLoadAction) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) Session(org.hibernate.Session)

Aggregations

AfterLoadAction (org.hibernate.loader.spi.AfterLoadAction)7 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 LockOptions (org.hibernate.LockOptions)3 Session (org.hibernate.Session)3 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)3 Loadable (org.hibernate.persister.entity.Loadable)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 LockMode (org.hibernate.LockMode)2 CallableStatement (java.sql.CallableStatement)1 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 HibernateException (org.hibernate.HibernateException)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 RowSelection (org.hibernate.engine.spi.RowSelection)1 EventSource (org.hibernate.event.spi.EventSource)1 PostLoadEvent (org.hibernate.event.spi.PostLoadEvent)1 PreLoadEvent (org.hibernate.event.spi.PreLoadEvent)1