/**
  * Attempts to resolve the entity id corresponding to the event's natural id values from the
  * session
  *
  * @param event The load event
  * @return The entity from the cache, or null.
  */
 protected Serializable resolveFromCache(final ResolveNaturalIdEvent event) {
   return event
       .getSession()
       .getPersistenceContext()
       .getNaturalIdHelper()
       .findCachedNaturalIdResolution(
           event.getEntityPersister(), event.getOrderedNaturalIdValues());
 }
  /**
   * Coordinates the efforts to load a given entity. First, an attempt is made to load the entity
   * from the session-level cache. If not found there, an attempt is made to locate it in
   * second-level cache. Lastly, an attempt is made to load it directly from the datasource.
   *
   * @param event The load event
   * @return The loaded entity, or null.
   */
  protected Serializable resolveNaturalId(final ResolveNaturalIdEvent event) {
    final EntityPersister persister = event.getEntityPersister();

    final boolean traceEnabled = LOG.isTraceEnabled();
    if (traceEnabled)
      LOG.tracev(
          "Attempting to resolve: {0}",
          MessageHelper.infoString(
              persister, event.getNaturalIdValues(), event.getSession().getFactory()));

    Serializable entityId = resolveFromCache(event);
    if (entityId != null) {
      if (traceEnabled)
        LOG.tracev(
            "Resolved object in cache: {0}",
            MessageHelper.infoString(
                persister, event.getNaturalIdValues(), event.getSession().getFactory()));
      return entityId;
    }

    if (traceEnabled)
      LOG.tracev(
          "Object not resolved in any cache: {0}",
          MessageHelper.infoString(
              persister, event.getNaturalIdValues(), event.getSession().getFactory()));

    return loadFromDatasource(event);
  }
  /**
   * Performs the process of loading an entity from the configured underlying datasource.
   *
   * @param event The load event
   * @return The object loaded from the datasource, or null if not found.
   */
  protected Serializable loadFromDatasource(final ResolveNaturalIdEvent event) {
    final SessionFactoryImplementor factory = event.getSession().getFactory();
    final boolean stats = factory.getStatistics().isStatisticsEnabled();
    long startTime = 0;
    if (stats) {
      startTime = System.currentTimeMillis();
    }

    final Serializable pk =
        event
            .getEntityPersister()
            .loadEntityIdByNaturalId(
                event.getOrderedNaturalIdValues(), event.getLockOptions(), event.getSession());

    if (stats) {
      final NaturalIdRegionAccessStrategy naturalIdCacheAccessStrategy =
          event.getEntityPersister().getNaturalIdCacheAccessStrategy();
      final String regionName =
          naturalIdCacheAccessStrategy == null
              ? null
              : naturalIdCacheAccessStrategy.getRegion().getName();

      factory
          .getStatisticsImplementor()
          .naturalIdQueryExecuted(regionName, System.currentTimeMillis() - startTime);
    }

    // PK can be null if the entity doesn't exist
    if (pk != null) {
      event
          .getSession()
          .getPersistenceContext()
          .getNaturalIdHelper()
          .cacheNaturalIdCrossReferenceFromLoad(
              event.getEntityPersister(), pk, event.getOrderedNaturalIdValues());
    }

    return pk;
  }
 @Override
 public void onResolveNaturalId(ResolveNaturalIdEvent event) throws HibernateException {
   final Serializable entityId = resolveNaturalId(event);
   event.setEntityId(entityId);
 }