コード例 #1
0
 private void cascadeOnLock(LockEvent event, EntityPersister persister, Object entity) {
   EventSource source = event.getSession();
   source.getPersistenceContext().incrementCascadeLevel();
   try {
     new Cascade(CascadingActions.LOCK, CascadePoint.AFTER_LOCK, source)
         .cascade(persister, entity, event.getLockOptions());
   } finally {
     source.getPersistenceContext().decrementCascadeLevel();
   }
 }
コード例 #2
0
  /**
   * Handle the given lock event.
   *
   * @param event The lock event to be handled.
   * @throws HibernateException
   */
  public void onLock(LockEvent event) throws HibernateException {

    if (event.getObject() == null) {
      throw new NullPointerException("attempted to lock null");
    }

    if (event.getLockMode() == LockMode.WRITE) {
      throw new HibernateException("Invalid lock mode for lock()");
    }

    if (event.getLockMode() == LockMode.UPGRADE_SKIPLOCKED) {
      LOG.explicitSkipLockedLockCombo();
    }

    SessionImplementor source = event.getSession();

    Object entity = source.getPersistenceContext().unproxyAndReassociate(event.getObject());
    // TODO: if object was an uninitialized proxy, this is inefficient,
    //      resulting in two SQL selects

    EntityEntry entry = source.getPersistenceContext().getEntry(entity);
    if (entry == null) {
      final EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
      final Serializable id = persister.getIdentifier(entity, source);
      if (!ForeignKeys.isNotTransient(event.getEntityName(), entity, Boolean.FALSE, source)) {
        throw new TransientObjectException(
            "cannot lock an unsaved transient instance: " + persister.getEntityName());
      }

      entry = reassociate(event, entity, id, persister);
      cascadeOnLock(event, persister, entity);
    }

    upgradeLock(entity, entry, event.getLockOptions(), event.getSession());
  }