protected PersistenceException wrapLockException(HibernateException e, LockOptions lockOptions) {
   final PersistenceException pe;
   if (e instanceof OptimisticEntityLockException) {
     final OptimisticEntityLockException lockException = (OptimisticEntityLockException) e;
     pe =
         new OptimisticLockException(
             lockException.getMessage(), lockException, lockException.getEntity());
   } else if (e instanceof org.hibernate.exception.LockTimeoutException) {
     pe = new LockTimeoutException(e.getMessage(), e, null);
   } else if (e instanceof PessimisticEntityLockException) {
     final PessimisticEntityLockException lockException = (PessimisticEntityLockException) e;
     if (lockOptions != null && lockOptions.getTimeOut() > -1) {
       // assume lock timeout occurred if a timeout or NO WAIT was specified
       pe =
           new LockTimeoutException(
               lockException.getMessage(), lockException, lockException.getEntity());
     } else {
       pe =
           new PessimisticLockException(
               lockException.getMessage(), lockException, lockException.getEntity());
     }
   } else if (e instanceof org.hibernate.PessimisticLockException) {
     final org.hibernate.PessimisticLockException jdbcLockException =
         (org.hibernate.PessimisticLockException) e;
     if (lockOptions != null && lockOptions.getTimeOut() > -1) {
       // assume lock timeout occurred if a timeout or NO WAIT was specified
       pe = new LockTimeoutException(jdbcLockException.getMessage(), jdbcLockException, null);
     } else {
       pe = new PessimisticLockException(jdbcLockException.getMessage(), jdbcLockException, null);
     }
   } else {
     pe = new OptimisticLockException(e);
   }
   return pe;
 }
 public PersistenceException wrapLockException(HibernateException e, LockOptions lockOptions) {
   PersistenceException pe;
   if (e instanceof org.hibernate.OptimisticLockException) {
     org.hibernate.OptimisticLockException ole = (org.hibernate.OptimisticLockException) e;
     pe = new OptimisticLockException(ole.getMessage(), ole, ole.getEntity());
   } else if (e instanceof org.hibernate.PessimisticLockException) {
     org.hibernate.PessimisticLockException ple = (org.hibernate.PessimisticLockException) e;
     if (lockOptions != null && lockOptions.getTimeOut() > -1) {
       // assume lock timeout occurred if a timeout or NO WAIT was specified
       pe = new LockTimeoutException(ple.getMessage(), ple, ple.getEntity());
     } else {
       pe = new PessimisticLockException(ple.getMessage(), ple, ple.getEntity());
     }
   } else {
     pe = new OptimisticLockException(e);
   }
   return pe;
 }
 @Override
 protected String applyLocks(
     String sql,
     QueryParameters parameters,
     Dialect dialect,
     List<AfterLoadAction> afterLoadActions)
     throws QueryException {
   // can't cache this stuff either (per-invocation)
   final LockOptions lockOptions = parameters.getLockOptions();
   final String result;
   if (lockOptions == null
       || (lockOptions.getLockMode() == LockMode.NONE && lockOptions.getAliasLockCount() == 0)) {
     return sql;
   } else {
     LockOptions locks = new LockOptions();
     locks.setLockMode(lockOptions.getLockMode());
     locks.setTimeOut(lockOptions.getTimeOut());
     locks.setScope(lockOptions.getScope());
     Iterator iter = lockOptions.getAliasLockIterator();
     while (iter.hasNext()) {
       Map.Entry me = (Map.Entry) iter.next();
       locks.setAliasSpecificLockMode(
           getAliasName((String) me.getKey()), (LockMode) me.getValue());
     }
     Map keyColumnNames = null;
     if (dialect.forUpdateOfColumns()) {
       keyColumnNames = new HashMap();
       for (int i = 0; i < names.length; i++) {
         keyColumnNames.put(names[i], persisters[i].getIdentifierColumnNames());
       }
     }
     result = dialect.applyLocksToSql(sql, locks, keyColumnNames);
   }
   logQuery(queryString, result);
   return result;
 }